Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 cooling their heels in the Monastery: (3)
As of 2025-12-12 23:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (92 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.