http://qs321.pair.com?node_id=664987


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>