my @set = qw(a b c); my %uniq; my @comb = grep !$uniq{$_}++, map join(",",sort split(//,$_)), glob((" +{".join(",",@set)."}") x @set); print "$_\n" for @comb; __END__ a,a,a a,a,b a,a,c a,b,b a,b,c a,c,c b,b,b b,b,c b,c,c c,c,c
Update: here's a slightly modified version which can handle not just single letters items (which would have been a problem with the first version...) — pick a separator as appropriate.
my @set = qw(foo bar baz); # use glob to expand all permutations my @perm = glob( join(",", ("{".join(",",@set)."}") x @set) ); print "Permutations:\n"; print "$_\n" for @perm; # filter out duplicates my %uniq; my @comb = grep !$uniq{$_}++, map join(",",sort split(/,/,$_)), @perm; print "Combinations:\n"; print "$_\n" for @comb;
In reply to Re: finding combinations and removing selective duplicates
by almut
in thread finding combinations and removing selective duplicates
by targetsmart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |