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

Re: [PBML] Swapping elements in an array

Expand Messages
  • Jenda Krynicky
    From: lord_vodka_ccccccxxxxxxvi ... You want to review the docs for sort() and rand(). To swap two elements of an array
    Message 1 of 4 , Dec 5, 2006
    • 0 Attachment
      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.