Re: [PBML] Swapping elements in an array
- 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
hope it help
For odd number of elements it need to be customize.
On 04 Dec 2006 18:17:44 -0800, merlyn@... <merlyn@...>
>[Non-text portions of this message have been removed]
> >>>>> "lord" == lord vodka ccccccxxxxxxvi <
> lord> Let's say I have an array (named @TONE_ROW) with twelve elements in
> 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
> <merlyn@... <merlyn%40stonehenge.com>> <URL:
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
- 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