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

Re: N Permutations of M Items

by atcroft (Abbot)
on Dec 20, 2002 at 01:25 UTC ( [id://221315]=note: print w/replies, xml ) Need Help??


in reply to N Permutations of M Items

Don't know of a module, but if you are just looking at strings, the following code would work, printing each generated possibility only once:

# # Assumes $word and $desired_length previously defined. # my @characters = split('', $word); my (%found); &gen_nofm(\@characters, \%found, '', $desired_length); print($_, "\n") foreach (sort({lc($a) cmp lc($b)} keys(%found))); sub gen_nofm { my ($charlist, $found_thusly, $partial_word, $desired_length) = @_; if ($desired_length) { for (my $index = 0; $index < scalar(@{$charlist}); $index++) { &gen_nofm($charlist, $found_thusly, $partial_word . ${$charlist}[$index], $desired_length - 1); } } else { ${$found_thusly}{$partial_word}++; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found