Above my head:
use strict;
use warnings;
use Data::Dumper;
print Dumper(distribute(7, [1..30]));
sub distribute {
my ($n, $array) = @_;
my @parts;
my $i = 0;
foreach my $elem (@$array) {
push @{ $parts[$i++ % $n] }, $elem;
};
return \@parts;
};
This ensures that the number of elements in @parts can only differ by one. There is another solution that pre-calculated numbers and used splicing:
push @parts, [ @$array[$offset..$offset+$chunk] ];
$offset += chunk;
source
share