http://www.perlmonks.org?node_id=1142143


in reply to RFC: Playing with glob

A google search for "perl combinations and permutations" yielded Permutations and combinations. Since you are creating permutations of the given arrays, the name of the subroutine should probably be permute:

print "[", join(", ", @$_), "]\n" for permute( [qw( red green blue )], [qw( north south east west )], [1 .. 3], ); # authored by merlyn sub permute { my $last = pop @_; unless (@_) { return map [$_], @$last; } return map { my $left = $_; map [@$left, $_], @$last } permute(@_); } __DATA__ [red, north, 1] [red, north, 2] [red, north, 3] [red, south, 1] [red, south, 2] [red, south, 3] [red, east, 1] [red, east, 2] [red, east, 3] [red, west, 1] [red, west, 2] [red, west, 3] [green, north, 1] [green, north, 2] [green, north, 3] [green, south, 1] [green, south, 2] [green, south, 3] [green, east, 1] [green, east, 2] [green, east, 3] [green, west, 1] [green, west, 2] [green, west, 3] [blue, north, 1] [blue, north, 2] [blue, north, 3] [blue, south, 1] [blue, south, 2] [blue, south, 3] [blue, east, 1] [blue, east, 2] [blue, east, 3] [blue, west, 1] [blue, west, 2] [blue, west, 3]

As you can see, his method returns a list of permutations, and each permutation is contained inside an array reference. By doing this, he has deferred the decision of formatting the output to the client and he does not have to worry about that portion of the code, which has been distilled to a simple print "[", join(", ", @$_), "]\n" for permute().

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)