Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Swapping elements in an array

Expand Messages
  • Nawab Shaikh
    Hi you can simply write a logic as per any traditional language. for i =0 to lenght of the array temp = array(i) array (i) = array(i+1) array (i+1) = temp
    Message 1 of 4 , Dec 5, 2006
      Hi you can simply write a logic as per any traditional language.

      for i =0 to lenght of the array
      temp = array(i)
      array (i) = array(i+1)
      array (i+1) = temp
      i+=2
      loop

      hope it help

      For odd number of elements it need to be customize.
      thanks
      Nawab


      On 04 Dec 2006 18:17:44 -0800, merlyn@... <merlyn@...>
      wrote:
      >
      > >>>>> "lord" == lord vodka ccccccxxxxxxvi <
      > lord_vodka_ccccccxxxxxxvi@...<lord_vodka_ccccccxxxxxxvi%40yahoo.com>>
      > writes:
      >
      > lord> Let's say I have an array (named @TONE_ROW) with twelve elements in
      > it.
      >
      > Let's say you have a homework assignment, and you wanted to cheat on it.
      > Maybe you could just post it here!
      >
      > --
      > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
      > 0095
      > <merlyn@... <merlyn%40stonehenge.com>> <URL:
      > http://www.stonehenge.com/merlyn/>
      > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
      > training!
      >
      >


      [Non-text portions of this message have been removed]
    • Jenda Krynicky
      From: lord_vodka_ccccccxxxxxxvi ... You want to review the docs for sort() and rand(). To swap two elements of an array
      Message 2 of 4 , Dec 5, 2006
        From: "lord_vodka_ccccccxxxxxxvi" <lord_vodka_ccccccxxxxxxvi@...>

        > Let's say I have an array (named @TONE_ROW) with twelve elements in
        > it. Now how would I do the following things to it? Reverse the order
        > of the elements (@TONE_ROW[0] becomes @TONE_ROW[11], @TONE_ROW[1]
        > becomes @TONE_ROW[10], etc.)
        >
        > Swap elements 0 and 1, 2 and 3, 4 and 5, 6 and 7, 8 and 9, 10 and 11
        > Shuffle the array (with fairly random results) Swap the values of the
        > Highest and lowest, second highest and second lowest, third highes and
        > third lowest and so on.

        You want to review the docs for sort() and rand().

        To swap two elements of an array (or the values of two variables) you
        can do this:

        ($ARR[1], $ARR[3]) = ($ARR[3], $ARR[1]);
        or with variables
        ($x, $y) = ($y, $x);

        With Perl you do not need any temporary variable, you can assign to
        multiple variables or array slots or hash values at once so you just
        specify the list of values and assign it to the list of
        variables/array slots/hash values.

        Jenda

        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      Your message has been successfully submitted and would be delivered to recipients shortly.