Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Rosetta code: Split an array into chunks

by shevek (Beadle)
on Oct 23, 2010 at 01:31 UTC ( #866914=note: print w/replies, xml ) Need Help??


in reply to Rosetta code: Split an array into chunks

How about an iterator for it?
sub gen_group_array { my ($group,$array) = @_; my ($start,$end) = (0,$group - 1); return sub { my $str = join ' ',@$array[$start..$end]; $start += $group; $end += $group; return $str; }; } my @array = qw(a b c d e f); my $group_size = 2; my $grouping = gen_group_array($group_size,\@array); print $grouping->() . "\n" for 1 .. ($#array+1)/$group_size;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-08-02 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (21 votes). Check out past polls.

    Notices?