rje has asked for the wisdom of the Perl Monks concerning the following question:
Brothers and Sisters:
My code cycles thru the elements of an array, sort of like rotating bits in a register. The important bit is the 'cycle' function -- it rotates or cycles thru the array without emptying it. Array order doesn't need to be preserved. A functional sample would be:
my $stuff = [ 'a', 'b', 'c', 'd' ]; sub cycle { push @{$_[0]}, shift @{$_[0]}; return $_[0]->[-1]; } print cycle $stuff for 1..30; # result: # $ perl cycle.pl # abcdabcdabcdabcdabcdabcdabcdab
I'm wondering if there's a better way to do it. Anyone have any suggestions?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is there a "better" way to cycle an array?
by choroba (Cardinal) on Sep 07, 2013 at 19:21 UTC | |
|
Re: Is there a "better" way to cycle an array?
by hdb (Monsignor) on Sep 07, 2013 at 20:26 UTC | |
|
Re: Is there a "better" way to cycle an array?
by Eily (Monsignor) on Sep 07, 2013 at 21:57 UTC | |
|
Re: Is there a "better" way to cycle an array?
by kcott (Archbishop) on Sep 08, 2013 at 06:23 UTC | |
|
Re: Is there a "better" way to cycle an array?
by Preceptor (Deacon) on Sep 09, 2013 at 11:08 UTC | |
|
Re: Is there a "better" way to cycle an array?
by Anonymous Monk on Sep 08, 2013 at 16:02 UTC |