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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|