If you are going to be frequently searching for anagrams then it is worth while splitting it into two scripts, the first reads the dictionary, generates the look up table and then dumps the table out to disk as a perl data structure. The second script reads in the data structure and looks up the word. The advantage is that you only generate the lookup table when the dictionary is updated.
.Script to create the look up table.
and to find the anagramsuse strict; use warnings; use YAML::Syck qw(DumpFile); my $dict_file = 'EN_AU.dic'; open my $dict, '<', $dict_file or die "Unable to open '$dict_file' for + reading: $!"; my %word_table; while (<$dict>) { chomp $word; my $key = join '', sort map lc(), split //, $word; push @{$word_table{$key}}, $word; } DumpFile( 'dict.yml', \%word_table );
use strict; use warnings; use YAML::Syck qw(LoadFile; # slurp all arguments my @letters = split //, @ARGV; die usage unless @letters; my $key = join '', sort @letters; my $lookup_word = LoadFile('dict.yml'); if ( exists $lookup_word->{$key} ) { my @words = sort @{ $lookup_words{$key} }; print "Found words: @words\n; } else { print "No words found\n"; } sub usage { ... }
In reply to Re: Found a word from a set of letters
by hipowls
in thread Found a word from a set of letters
by vnpenguin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |