http://www.perlmonks.org?node_id=861982


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