This solution also depends on a dictionary, but it doesn't read the whole thing into memory. It tests each line of it as it reads through.

my $dictionary = '/usr/share/dict/words'; # input letters, changed to lowercase my @letters = split //, lc shift @ARGV; open my $dict_fh, '<', $dictionary or die "Can't read dictionary '$dictionary': $!"; WORD: while ( my $word = lc <$dict_fh> ) { chomp $word; # $word is lowercase with no newline my $orig_word = $word; # for each letter in the search set, for my $letter ( @letters ) { # remove that letter from the dictionary word # and skip to the next word if it's not found next WORD if ! ($word =~ s/$letter//); } # if all the dictionary word's letters were used, # print out the original dictionary word. print "$orig_word\n" if $word eq ''; } close $dict_fh or die "Can't close: $!";

Update: Added some comments to describe the algorithm (thanks to Limbic~Region for the suggestion). Also, this code is tested (with the case the OP specified).


In reply to Re: Found a word from a set of letters by kyle
in thread Found a word from a set of letters by vnpenguin

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.