Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
What I want to do is to create some loop mechanism that allows
me to iterate through an @array.
The starting position of the loop is calculated from $offset_value.
$offset_value will be between 0 and (scalar @array) -1
e.g
Then I would want get the following values returned@array = (qw/ Fred Mark Joe Mary Paul /); $offset_value = 0
incrementing $offset then I want to getFred Mark Joe Mary Paul
Essentially the loop moves the last element to become the first.Paul Fred Mark Joe Mary
Any Clues/hints/help much appreciated.my @array=(qw/ Fred Mark Joe Mary Paul /); my $offset=0; my $loop=0; while ($loop < 10){ # loop is just to increment the offset value # main loop below for (my $i=0;$i<scalar @array;$i++){ my $r = $i + $offset; if ($r > scalar @array-1){ $r = $r - (scalar @array); } print @array[$r]; } $offset++; $offset=($offset>(scalar @array -1)?0:$offset; $loop++; }
update (broquaint): tidied up formatting
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: looping backwards
by eyepopslikeamosquito (Archbishop) on Jul 29, 2003 at 09:32 UTC | |
by Anonymous Monk on Jul 29, 2003 at 10:05 UTC | |
|
Re: looping backwards
by Abigail-II (Bishop) on Jul 29, 2003 at 13:45 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 30, 2003 at 03:19 UTC | |
|
Re: looping backwards
by RMGir (Prior) on Jul 29, 2003 at 09:20 UTC |