CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr
 MAIN INDEX SEARCHPOSTS WHO'S ONLINE LOG IN

Home: Perl Programming Help: Intermediate:
How to generate Cartesian of all combination!!

gevni
Novice

Feb 12, 2014, 5:24 AM

Post #1 of 2 (2307 views)
 How to generate Cartesian of all combination!! Can't Post
Let me explain my problem with an example of set
 Code
`Let i have two sets A{1,2} B={1,2,3}  The Cartesian product AXB=2X3=6 so resultant sets are {1,1},{1,2},{1,3},{2,1},{2,2},{2,3}  I want the output where order also matter for example Total combinations of set A= 2! (factorial)=2 Total combination of set B= 3! (factorial)=6 and then i want AXB=12 {1,1},{1,2},{1,3},{1,1},{2,1},{3,1} {2,1},{2,2},{2,3},{1,2},{2,2},{3,2}`
I have this code
 Code
`sub Cartesian { #return one array contening one vector of index. #these index could be used to generate one vector of the cartesian product. #if shift function call without parameter it will use default array @_ for getting all the parameters of function   my \$i = shift; # function use to get the parameter of function, it return the first element of array @_     my \$ref_array = shift; #keep this copy (to avoid the copy before the call) #array with the size of each sub arrays   my @array = @{\$ref_array}; # way of getting all the variable of array      my @res;   my \$elem;   while(\$elem = shift @array){     push @res, \$i % \$elem;     \$i = \$i / \$elem;   }   return \@res; }`

 Code
`Cartesian(\$i,\@size_set);`

Can you plz help me how can i get all strings of combination by modifying the above code. As i am not too gud in perl. Thanks

Laurent_R
Veteran / Moderator

Feb 12, 2014, 11:05 AM

Post #2 of 2 (2292 views)
 Re: [gevni] How to generate Cartesian of all combination!! [In reply to] Can't Post
Hi, just a quick example one-liner:

 Code
`\$ perl -e 'my  @a = qw /1 2/; my  @b = qw /3 4 5/; for my \$first (@a) { print "[\$first, \$_]\n", "[\$_, \$first]\n" for @b;}' [1, 3] [3, 1] [1, 4] [4, 1] [1, 5] [5, 1] [2, 3] [3, 2] [2, 4] [4, 2] [2, 5] [5, 2]`

(I changed the digits in B so that it would be more obvious where the data is coming from.

If you need an array of arrays (AoA), just change the loop:

 Code
`\$ perl -e 'use Data::Dumper; my  @a = qw /1 2/; my  @b = qw /3 4 5/; my @c; for my \$first (@a) { push @c, [\$first, \$_], [\$_, \$first] for @b;}; print Dumper \@c;' \$VAR1 = [           [             '1',             '3'           ],           [             '3',             '1'           ],           [             '1',             '4'           ],           [             '4',             '1'           ],           [             '1',             '5'           ],           [             '5',             '1'           ],           [             '2',             '3'           ],           [             '3',             '2'           ],           [             '2',             '4'           ],           [             '4',             '2'           ],           [             '2',             '5'           ],           [             '5',             '2'           ]         ];`

Now, there are also some modules to do that, such as Algorithm::Combinatorics or Math::Combinatorics.

The glob function might also help.

(This post was edited by Laurent_R on Feb 12, 2014, 2:02 PM)

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0