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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |