Not sure that this is much of an improvement, but it only uses 5 for loops and one sort.
#!/usr/bin/perl -w
my $m = [];
$m->[0] = [1,2,3,4,0];
$m->[1] = [1,2,3,5,6];
$m->[2] = [1,2,3];
$m->[3] = [3,1,2];
$m->[4] = [3,2,1,0];
my $a = perms($m);
foreach my $i (@{ $a }) {
print "Item: $i\n";
}
print "Total: ", $#{ $a }, "\n";
exit;
sub perms {
my ($m) = shift;
my @a = ();
my $n = 0;
for my $a (0..$#{ $m->[0]}) {
for my $b (0..$#{ $m->[1] }) {
for my $c (0..$#{ $m->[2] }) {
for my $d (0..$#{ $m->[3] }) {
for my $e (0..$#{ $m->[4] }) {
push @a, $m->[0]->[$a].$m->[1]->[$b].$m->[2]->
+[$c].$m->[3]->[$d].$m->[4]->[$
e];
}
}
}
}
}
@a = sort @a;
return \@a;
}
but I agree with the idea of using the sort.
And you all beat me!