If speed is your concern, I would recommend using a pre-built regexp for fast filtering of the candidate words, and a slower exact test against a canonical (sorted letters) form.
Tested, working code:
use strict; use warnings; my $dict = '/usr/share/dict/words'; # Do not space-separate the word in this version. my $scrambled_word = shift or die "Must specify a word\n"; my $scrambled_length = length $scrambled_word; my $scrambled_sorted = join '', sort split '', $scrambled_word; my $pattern = qr{ \A (?: [$scrambled_word]{$scrambled_length} ) \n \z }x; open DICT, '<', $dict or die "Cannot open '$dict': $!"; while (<DICT>) { next unless /$pattern/o; chomp; my $sorted = join '', sort split '', $_; next unless $sorted eq $scrambled_sorted; print "$_\n"; } close DICT or warn;
In reply to Re: Word Unscrambler
by Util
in thread Word Unscrambler
by jonnybe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |