Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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;


Comment on Re: Rosetta code: Split an array into chunks
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
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 romping around the Monastery: (7)
As of 2014-10-02 01:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls