Re: [PBML] Swapping elements in an array
- From: "lord_vodka_ccccccxxxxxxvi" <lord_vodka_ccccccxxxxxxvi@...>
> Let's say I have an array (named @TONE_ROW) with twelve elements inYou want to review the docs for sort() and rand().
> it. Now how would I do the following things to it? Reverse the order
> of the elements (@TONE_ROW becomes @TONE_ROW, @TONE_ROW
> becomes @TONE_ROW, 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.
To swap two elements of an array (or the values of two variables) you
can do this:
($ARR, $ARR) = ($ARR, $ARR);
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@... === 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