Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Factorial Matrix

by dwm042 (Priest)
on Jan 29, 2008 at 20:49 UTC ( [id://664987]=note: print w/replies, xml ) Need Help??


in reply to Factorial Matrix

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 permute_all.pl Depth = 3 A1C A1D A1E A2C A2D A2E B1C B1D B1E B2C B2D B2E C:\Code>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://664987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2025-03-24 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (65 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.