Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

permutations?

by eduardo (Curate)
on Jun 14, 2000 at 00:00 UTC ( [id://17968]=perlquestion: print w/replies, xml ) Need Help??

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

let us say we have a hash of lists that looks like this:
my $words = { 'lng' => ['lang', 'long', 'leng', 'loo'], 'sentance' => ['sentence'], 'thxs' => ['this', 'thus'] };
and we want to run it through some transform that will give us an array of every single possible combination of words from each family of keys, in this case:
lang sentence this long sentence this leng sentence this loo sentence this lang sentence thus long sentence thus leng sentence thus loo sentence thus
not neccesseraly in that order, order is unimportant, as long as I had all the possible combinations... what is a good perlish way to do this?

Replies are listed 'Best First'.
RE: permutations?
by merlyn (Sage) on Jun 14, 2000 at 05:58 UTC
    Untested, but I often get this stuff right on the first try... :)
    my $words = { 'lng' => ['lang', 'long', 'leng', 'loo'], 'sentance' => ['sentence'], 'thxs' => ['this', 'thus'] }; print map "$_\n", phol($words); sub phol { my($key, $value, @rest) = %{+shift}; if (@rest) { return map { my $r = $_; map { "$_ $r" } @$value } phol({@rest}); } else { return @$value; } }

    -- Randal L. Schwartz, Perl hacker

      Is there any easier way to get values listed as permutations? I mean I can't use those Perl things at all. And I would like to get all 120 permutations of five letters listed. Is there any way find such program without programming? jamkoi@surfeu.fi
RE: permutations?
by sean (Beadle) on Jun 14, 2000 at 05:06 UTC
    Hmmm...
    my %words = ( 'lng' => ['lang','long','leng','loo'], 'sentance' => ['sentence'], 'thxs' => ['this','thus'] ); my @results; for my $key (keys %words) { if (defined(@results)) { @results = &loljoin(\@results, $words{$key}); } else { @results = map {[$_]} @{$words{$key}}; } } sub loljoin { #pass me two references to arrays: my @results = @{(shift)}; #the results so far.. my @newwords = @{(shift)}; #and the set to add to our results my @newresults; for my $word (@newwords) { for my $result (@results) { push(@newresults,[@$result, $word]); } } return @newresults; } # results are in @results as a list of lists # eg: for my $foo { print join(" ",@$foo) . "\n" }
    Hmm, well. That appears to work, but it doesn't look as good as it could.
Re: permutations?
by btrott (Parson) on Jun 14, 2000 at 00:14 UTC
Re: permutations?
by plaid (Chaplain) on Jun 14, 2000 at 00:38 UTC
    You say that order is unimportant, but do you mean that the sentences can be in any order at all? You're using a hash reference to store the keys 'lng', 'sentance', and 'thxs', but hashes don't preserve any order whatsoever, so you're likely not going to get the keys in the same order you entered them into $words. If you want to keep the sentence parts in the same order, you might want to instead make it an array reference
    my $words = [ ['lang', 'long', 'leng', 'loo'], ['sentence'], ['this', 'thus'] ];
      the order of the words isn't important at all, as long as one word from every key becomes part of each candidate answer... and unfortunately they are being passed to me as a hash of lists, so, i have to deal with them that way. (from another object) however, in the answer i can definetly mangle it any way we see fit...
Re: permutations?
by fglock (Vicar) on Jul 09, 2002 at 14:06 UTC

    You could install module Algorithm::Permute, then try this:

    use Algorithm::Permute; my $p = new Algorithm::Permute(['a'..'e']); while (@res = $p->next) { print join(", ", @res), "\n"; }
Re: permutations?
by shotgunefx (Parson) on Jul 10, 2002 at 09:42 UTC
    You could use this snippet like so.
    my $iter = make_permutator( values%{$words} ); while (my @els = $iter->() ){ print join (" ",@els)"\n"; }


    -Lee

    "To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://17968]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-12-08 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.