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 johngg (Abbot)
on Sep 25, 2010 at 18:11 UTC ( #861982=note: print w/ replies, xml ) Need Help??


in reply to Rosetta code: Split an array into chunks

This seems to work.

knoppix@Microknoppix:~$ perl -E ' > @l = qw{ a bb c d e f g h }; > say sub { join q{ }, @_ }->( > grep defined, map shift @l, 1 .. 3 > ) while @l;' a bb c d e f g h knoppix@Microknoppix:~$

I also piped the output through hexdump to check that there were no trailing spaces (which you do get without the grep).

Update: Using List::Util::min() to remove the need for the grep.

knoppix@Microknoppix:~$ perl -MList::Util=min -E ' > @l = qw{ a bb c d e f g h }; > say sub { join q{ }, @_ }->( > map shift @l, 1 .. min( 3, scalar @l ) > ) while @l;' a bb c d e f g h knoppix@Microknoppix:~$

Cheers,

JohnGG


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-18 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (113 votes), past polls