|
|
FAQ 4.47 How do I handle circular lists?
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.47: How do I handle circular lists?
(contributed by brian d foy)
If you want to cycle through an array endlessly, you can increment the
index modulo the number of elements in the array:
my @array = qw( a b c );
my $i = 0;
while( 1 ) {
print $array[ $i++ % @array ], "\n";
last if $i > 20;
}
You can also use "Tie::Cycle" to use a scalar that always has the next
element of the circular array:
use Tie::Cycle;
tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF 000000 FFFF00 ) ];
print $cycle; # FFFFFF
print $cycle; # 000000
print $cycle; # FFFF00
The "Array::Iterator::Circular" creates an iterator object for circular
arrays:
use Array::Iterator::Circular;
my $color_iterator = Array::Iterator::Circular->new(
qw(red green blue orange)
);
foreach ( 1 .. 20 ) {
print $color_iterator->next, "\n";
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
|
|
0
|
|
|
|
Reply
|
brian (1246)
|
6/4/2010 4:00:01 PM |
|
|
0 Replies
318 Views
(page loaded in 0.115 seconds)
Similiar Articles: Getting a hConsoleOutput handle - comp.os.ms-windows.programmer ...Is there any way of getting a handle to the console of a ... FAQ, and you need to start with the answer to item #12 ... 4/23/2010 12:15:47 PM How do I stop the pdksh command line from jumping? - comp.os.linux ...It is on my to do list to look at that. Mark. ... changes that way the edit modes handle ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ... Specifying command line parameters for ntpd running as a w2k ...... to handle if the NTP service and installer would handle ... Reply: Martin: 1/21/2005 3:47:12 PM ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ... export does not work on makefiles - comp.unix.programmer ...4) the '.' or 'source' operation of most shells can be ... fi' USR1 If the parent shell has registered to handle ... FAQ exist for this NG? - comp.lang.java.gui Does an FAQ ... Full example of memoir documentclass preamble - comp.text.tex ...It is easier to handle that instead of starting from... ... remove RTFSIGNATURE from email address) LaTeX FAQ ... On Dec 9, 6:47=A0am, Oscar Rotava <rot...@petrobras.com ... Does anybody know how to start a newsgroup for communication ...9/6/2003 7:47:21 PM ... Cheers Bhaskar > > To be easy, I do list-up ... Getting a hConsoleOutput handle - comp.os.ms-windows.programmer ... FAQ, and you need ... Using tape autochanger? - comp.sys.hp.hpux... apparently, mtx in its unaltered state, cannot deal with ... 52 seconds media changer timeout on tapes 1 47 (2 ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ... Where can I get one? - comp.sys.hp48... already been found (so I am a pessimist---I DEAL with ... William: 10/16/2003 2:47:56 PM ... CHP-Frequently Asked Questions of the Highway Patrol Where can I get one? Concatenate large number of matrices - comp.soft-sys.matlab ...James: 4/28/2010 4:47:04 PM ... is strongly discouraged (see question 4.6 in the newsgroup > FAQ if ... comp.soft-sys.matlab Hi, I need to deal with a very ... perlfaq4 - perldoc.perl.orgHow do I handle linked lists? How do I handle circular lists? How do I shuffle an array randomly? ... This section of the FAQ answers questions related to manipulating ... FAQ - MATLAB Wiki47 Comments; FAQ Edit History ... This is a list of frequently asked questions (with answers ... Handle objects do exhibit reference behavior when ... 7/2/2012 4:45:03 PM
|
|
|
|
|
|
|
|
|