in reply to Module Submission for CPAN
This is good code and will fill a hole on the CPAN. I was
looking for something similar several months ago and came up
with nothing.
I have a couple of suggestions that I hope will be helpful.
- Firstly, I believe there is an alternative data structure
that will really speed up your lookups. Here is my
suggested algorithm for building it:
s/\W//g, push @{$dict{join "", sort split //, lc}}, $_ for <DICT>;
In other words, sort each word and store it in its canonical form in the lookup table, so all anagrams are stored together. Then, when you want to check for anagrams of a word, you just go straight to its canonical key in the hash.
This might make your starts_word slower/less clear, but I'm not sure. Of course, starts_word won't be necessary for anagram lookups anymore, but it would be a useful standalone function.
- You'll probably want to do something more robust than assume /usr/dict/words. There are MakeMaker settings to play around with, but I imagine it will be a pain. Somehow, though you'll have to at least make a way for the user to specify where their dictionary is.
- You probably shouldn't use a global for the lookup table.
- You don't have to put that initialization code in a BEGIN block.
- $VERSION is almost always in all caps.
- Am I missing something, or does walk_dict() not seem to exist?
-dlc
In Section
Seekers of Perl Wisdom