Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How do I loop through a list two or more elements at a time?

by petral (Curate)
on Oct 25, 2001 at 20:58 UTC ( [id://121493]=note: print w/replies, xml ) Need Help??


in reply to How do I loop through a list two or more elements at a time?

To be pedantic, the question was "two or more".
Here's a conventional answer which allows for varying the grouping size. (Just to make it a little interesting, I included an alternate method for dealing with the last group being short.)
my@a=qw(a b c d e f); my $grpsiz = 2; if (@a% $grpsiz) { push @a, ("") x ($grpsiz - @a % $grpsiz) } for (my$i = 0; $i <= $#a; $i += $grpsiz) { print @a[$i .. $i+$grpsiz-1] }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://121493]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found