Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Factorial Matrix

by isutommyt (Initiate)
on Jan 29, 2008 at 19:21 UTC ( #664963=perlquestion: print w/replies, xml ) Need Help??

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

I'm not sure why I can't think today, but here is my problem.

I have a file that may look like this:

I need an output file that creates all possible permutations from that file:
... B,2,E (12 total possibilities)

Of course the original file can have a variable amount of "rows" and "columns". Help me monks! Thanks!

Replies are listed 'Best First'.
Re: Factorial Matrix
by ikegami (Patriarch) on Jan 29, 2008 at 20:00 UTC

    That's a cross product, not listing permutations or combinations.

    It's trivial with Algorithm::Loops's NestedLoops.

    use Algorithm::Loops qw( NestedLoops ); my @array1 = qw( A B ); my @array2 = qw( 1 2 ); my @array3 = qw( C D E ); my $iter = NestedLoops([ \@array1, \@array2, \@array3, ]); while (my @row = $iter->()) { print("@row\n"); }
Re: Factorial Matrix
by moritz (Cardinal) on Jan 29, 2008 at 19:25 UTC
Re: Factorial Matrix
by Limbic~Region (Chancellor) on Jan 29, 2008 at 19:33 UTC
    Please note that what you are asking for is a specific type of combinations not permutations. In permutations, order is important so "A, 1, C" is not the same as "A, C, 1". I say a specific type of combination because you are asking for all possible combinations selecting 1 item from each group - a more general combination may have "C, D, 2".

    One module has already been suggested. I would suggest Algorithm::Loops and tell you that solving the problem yourself is quite fun.

    Cheers - L~R

Re: Factorial Matrix
by dwm042 (Priest) on Jan 29, 2008 at 20:49 UTC
    This solution stores the data as an array of arrays and then uses a recursive function to deliver the solution.

    #!/usr/bin/perl use warnings; use strict; # read data into an array of arrays. my @AoA = (); while(<DATA>) { chomp; my @data = split /\,/, $_; push @AoA, [ @data ]; } # determine the depth of the array. my $depth = scalar @AoA; print "Depth = $depth\n"; # use a recursive solution to handle arbitrary depth of the data. permute ( \@AoA, "", 0 ); sub permute { my $aref = shift; my $string = shift; my $index = shift; if ( $index >= $depth ) { print $string, "\n"; return; } for (@{$aref->[$index]} ) { my $newstring = $string . $_; permute( $aref, $newstring, $index + 1) } } __DATA__ A,B 1,2 C,D,E
    C:\Code>perl Depth = 3 A1C A1D A1E A2C A2D A2E B1C B1D B1E B2C B2D B2E C:\Code>
Re: Factorial Matrix
by artist (Parson) on Jan 29, 2008 at 20:52 UTC
    List::MoreUtils also has some nice functions like 'zip','mash', 'pairwise' etc that you can use here.
Re: Factorial Matrix
by isutommyt (Initiate) on Jan 29, 2008 at 20:15 UTC
    Thanks for all the responses. The brain fart included just figuring out the name of what I wanted to do.

    Cartesian Cross-Products provided me exactly what I needed.

    Thanks Monks, you've come through again!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://664963]
Approved by moritz
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-11-30 03:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found