Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: how to seperate array after a certain number of elements?

by ikegami (Patriarch)
on Sep 20, 2007 at 20:24 UTC ( [id://640227]=note: print w/replies, xml ) Need Help??


in reply to Re: how to seperate array after a certain number of elements?
in thread how to seperate array after a certain number of elements?

Or you could go a step further and make an iterator.
sub break_by { my ($by, $data) = @_; my $i = 0; return sub { my $n = @$data - $i; $n = $n < 0 ? 0 : $n > $by ? $by : $n; $i += $n; return @$data[$i-$n..$i-1]; } } my @data = ( 1,2,3,4,5,6,7,8,9,10,11,12 ); my $iter = break_by(5, \@data); while (my @group = $iter->()) { print("@group\n"); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found