use Math::Combinatorics qw(combine); my @data = qw(A B C); my @frequency = qw(3 2 1); my $racklength = 2; my $combinat = Math::Combinatorics->new(count => $racklength, data => [@data], frequency => [@frequency], ); while(my @combo = $combinat->next_multiset){ print join(' ', @combo)."\n"; }