targetsmart has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I have to find answer for one of my requirement, if I give
./combinations.pl a b output: a,a a,b b,a b,b
i can increase the element in the set from a,b to a,b,c ... I have done one program for this, and it works for me
#!/usr/bin/perl ###################################################################### +########### #FILE : combinations.pl #USAGE : ./combinations.pl arguments # eg, # ./combinations +.pl a b # output: # a,a # a,b # b,a # b,b #DESCRIPTION : This program will give possible combinations or + elements # in a set, # eg, # if a,b is the input(ar +gument ot the program # the output is # a,a # a,b # b,a # b,b # the idea to create thi +s program is, when I wanted my program # to work for multiple c +onfigurations like a,b etc # then I have to look ou +t for atleast four combinations. #AUTHOR : Targetsmart #CREATED DATE : 17/12/2008 17:12:58 IST # ( NOTE : Date format is DD/MM/YYYY HH:MM:SS ) ###################################################################### +############ use strict; use warnings; use diagnostics; use Data::Dumper; use Clone qw(clone); # for deep copy of data structures our (@Buf,$Possibilities); if(@ARGV){ &process(&populatedata(@ARGV)); }else{ #my @actualdata= qw(connection no-connection); my @actualdata= qw(a b); #my @actualdata= (0..6); &process(&populatedata(@actualdata)); } ###################################################################### +########## # just a replication of actual array into number of arrays of same con +tent # the number of arrays depends on the size of the array # eg, # a,b is actual array # this function will prepare # [a,b], [a,b] # a,b,c is actual array # this function will prepare # [a,b,c], [a,b,c], [a,b,c] sub populatedata { my (@mainResult); push (@mainResult , [@_]) for 0..$#_; return \@mainResult; } ###################################################################### +########## ###################################################################### +########## # this recursive function which is take the replicated array like # [a,b,c], [a,b,c], [a,b,c] # and produce the possible combinations. # like # a,a,a # a,a,b # a,a,c # a,b,a # .... sub process() { my($Data) = shift; my $MyArray = shift @{$Data}; #print Dumper $MyArray; foreach my $Index (@{$MyArray}) { push(@Buf,$Index); # checking the next array if exists if(exists $Data->[0]){ # if there is a next array then supply the data # by doing a clone to retain the actual $data for # recursion, if not cloned then the actual data # reference will not be retained # eg, # consider the array or arrays # [a,b],[a,b] exist # when cloned # a,a # a,b after printing this # recursion will return # but the $Data is already emptied out not available for printing 'b' +combination # b,a # b,b # BUT if we clone # a,a # a,b after printing this # recursion will return # but the $Data is will contain [a,b] and available for printing 'b' c +ombination # b,a # b,b &process(clone($Data)); pop(@Buf); }else{ $"=","; # counting the combination sequence number. $Possibilities++; print "$Possibilities,@Buf\n"; pop @Buf; } } } ###################################################################### +##########
here is my problem?!
I need to remove the duplicates in output, eg, if the output contains (1,2,4) and (4,2,1) then I need to show only either one of them like only (1,2,4).
This question can also be asked like this, is any module available for removing duplicates in array of array ... of arrays?.
Please help me to solve this problem.

Replies are listed 'Best First'.
Re: finding combinations and removing selective duplicates
by toolic (Bishop) on Dec 17, 2008 at 17:09 UTC
    You could sort the elements, then store the stringified (comma-separated) sorted elements as hash keys, then print out just the hash keys:
    use strict; use warnings; my %uniqs; while (<DATA>) { chomp; my @list = split /,/; my @sorted = sort {$a <=> $b} @list; $uniqs{ join ',', @sorted }++; } print "$_\n" for keys %uniqs; __DATA__ 1,2,4 1,2,3 4,2,1

    prints:

    1,2,4 1,2,3
Re: finding combinations and removing selective duplicates
by almut (Canon) on Dec 17, 2008 at 17:39 UTC
    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;
Re: finding combinations and removing selective duplicates
by poolpi (Hermit) on Dec 18, 2008 at 11:49 UTC

    If you need to deal with combinatorial sequences :
    Algorithm::Combinatorics

    #!/usr/bin/perl use strict; use warnings; use Algorithm::Combinatorics qw(variations_with_repetition); my @data = qw(a b c); my $iter = variations_with_repetition( \@data, $#data ); while ( my $c = $iter->next ) { print @$c, "\n"; } __END__ Output: aa ab ac ba bb bc ca cb cc


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb