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---

Replies are listed 'Best First'.
Re^2: RFC: Playing with glob
by Lady_Aleena (Curate) on Sep 16, 2015 at 19:19 UTC

permute! Thank you for the much better name for the subroutine!

I was not looking for the best way to get all the permutations of the combinations of the arrays, I was seeing how that aspect of glob worked then fiddling with it. I first looked at glob for the file listing ability, then saw its ability to permute through several strings to make interesting lists and decided to play with it.

I am still interested in finding a way to incorporate a pattern into either to reduce the steps to getting the list of values.

Thank you for stopping by.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!