Perl: array_chunk() to split arrays into smaller chunks
I have an large array in Perl that I need in smaller chunks to make iteration easier. I borrowed a concept from PHP and implemented array_chunk()
in Perl.
my @orig = qw(foo bar baz one two three red yellow green donk);
my @new = array_chunk(3, @orig);
sub array_chunk {
my ($num, @arr) = @_;
my @ret;
while (@arr) {
push(@ret, [splice @arr, 0, $num]);
}
return @ret;
}