my $containers = ... my $objects = ... NestedLoops( [ (sub{ [0 .. $containers-1] }) x $objects ], sub { my @assignment = @_; my @bins; push @{ $bins[$assignment[$_]] }, $_ for 0 .. $objects-1; ## @bins now contains the groupings ## filter out if one of the bins is empty } );