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.

In reply to finding combinations and removing selective duplicates by targetsmart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.