Using a subroutine that takes a code block as its first argument and applies that code to groups of size specified in the second argument taken from the remaining arguments.
use strict; use warnings; use Data::Dumper; sub groupsOf (&$@); my $doubleStack = [ groupsOf { [ @_ ] } 3, qw{ a b c e 1 2 3 4 5 3 f } ]; print Data::Dumper ->Dumpxs( [ $doubleStack ], [ qw{ doubleStack } ] ); sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. ( @_ < $groupsOf ? @_ : $groupsOf ) ), @_ ? &$rcDoIt : (); }; &$rcDoIt; }
The output.
$doubleStack = [ [ 'a', 'b', 'c' ], [ 'e', '1', '2' ], [ '3', '4', '5' ], [ '3', 'f' ] ];
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: stack array to array of arrays
by johngg
in thread stack array to array of arrays
by spandox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |