If you're looking for a canned solution, try Math::Combinatorics (no personal experience).

However, this is a pretty fun problem to code. My brain finds it easiest to think of combinations recursively. For a list of letters, if the desired word length (n) is 1, just return the list. Otherwise iterate through each letter, and find all of the words of length that start with that letter (the current letter appended by all words found in the remaining letters, of length n - 1). Then find the list of words of length n that don't include the current letter. That's it. :) Pseudo code follows (let me know if you'd like a real Perl version; I have one kicking around in my sandbox, as well as a nonrecursive one).

function wordz ($n, @list ) {
  if ( n == 1 ) {
    return @list
  }
  for $current_letter ( @list ) {
    push @words, map { $current_letter . $_ }
      wordz( $n - 1, @letters_sans_current_letter;
    push @words, wordz( $n, @letters_sans_current_letter);
  }
  return @words;
}

-Colin.

WHITEPAGES.COM | INC


In reply to Re: scrambling all letters in a word until all combinations are found by cmeyer
in thread scrambling all letters in a word until all combinations are found by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.