Kratimoyporroon has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way, or good algorithm, in Perl, to find permutations? Let's say I have these sets, which I will read from a file. A = {1, 2, 3, 4, 5} B = {q, w, e, r, t} C = {junk, crap, spam, crud, kipple} D = ... (you get the idea) 1) There can be any number of sets. 2) The sets can have any numer of elements. I want to write an algorithm to deliver me permutations of said sets. For instance, a permutation of the above might be {A=1, B=q, C=kipple}. With these I intend to spawn off another process to do some work. Any idea on how best to do this in Perl? I'm fairly new and have not yet become proficient in "thinking in Perl". Thanks.

Replies are listed 'Best First'.
Re: Easy way to find permutations?
by BrowserUk (Patriarch) on Nov 03, 2002 at 02:01 UTC

    You'll find many other algorithms in Permutations and more still in Combinatorics.

    The first I found the first by typing "permutations" in the search box top left on every PerlMonks screen. The second (amongst others) by typing the same search string into the top field after clicking the super_search link also at the top of the screen


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: Easy way to find permutations?
by adrianh (Chancellor) on Nov 03, 2002 at 01:29 UTC
Re: Easy way to find permutations?
by Chady (Priest) on Nov 03, 2002 at 07:57 UTC
Re: Easy way to find permutations?
by shotgunefx (Parson) on Nov 03, 2002 at 09:21 UTC
    My own entry

    -Lee

    "To be civilized is to deny one's nature."
Re: Easy way to find permutations?
by tomhukins (Curate) on Nov 03, 2002 at 21:53 UTC
    If you had typed permutations into the search bar at the top of each page you would have found an answer to your question without having to wait for a response. You could use Super Search to find lots of discussions on how to deal with permutations and combinations in Perl.
      And if most people reading this knew what a permutation was they would have known that the original poster wasn't asking for permutations. Search engines are great..unless you don't actually know what you are supposed to be looking for.

      Of 5 solutions, the first nailed it, and the fourth was correct only because the poster of the link didn't know what a permutation was either. The other 3 - including your suggestion - were wrong because you were mislead by the word choice.

      Many other valid solutions exist. Quantum::Superpositions being an obfuscated one and

      for (glob '{1,2,3,4,5}\ {q,w,e,r,t}\ {junk,crap,spam,crud,kipple}') { print "$_\n"; }
      being a short one. Or you could try figuring out Re (tilly) 1 (perl): What Happened...(perils of porting from c)...
Re: Easy way to find permutations?
by jdporter (Paladin) on Nov 04, 2002 at 17:06 UTC
    Sounds a lot like a question on clpm I answered a while ago.

    The solution I offered at that time looked something like this:

    my @array = ( [ 1, 2, 3, 4, 5 ], [qw( q w e r t )], [qw( junk crap spam crud kipple )], ); my @all_combinations = combin( \@array ); # where combin() is defined as follows: sub combin { my( $ar, @stack ) = @_; return \@stack unless @$ar; my( $head, @a ) = @$ar; map { combin( \@a, @stack, $_ ) } @$head }