in reply to Fast wordlist lookup for game

Simply use a hash if memory is not an issue (you should test this.

my %dict; foreach my $word (@wordlist) { # or read from file ;-) $dict{$word} = 1; } if (exists $dict{'someWord'}) {...}

Feel free to use (or modify) this code

.

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re: Re: Fast wordlist lookup for game
by jerrygarciuh (Curate) on Oct 29, 2002 at 22:52 UTC
    Thanks for the reply but I have to say that memory always seems to be an issue if one uses expensive methods and the script gets heavily used at all.
    Thanks though,
    jg

      Use a hash to minimize the search time. Keep the hash on disk to minimize the memory usage (and start-up time if this is a CGI app.) I.e. Use a DBM. For speed, you'll probably find BerkeleyDB is best.

      -j
      -sauoq
      "My two cents aren't worth a dime.";
      
      ...oops, I guess I finished my post after you'd already seen a similar answer.

      You're right that it's expensive to keep the list in memory, but it will be the fastest, and we're not talking a lot of memory here (given a list with words on average the size of the words in the Unix dictionary).

      Reading things into the hash is not he most elegant solution, and someone will no doubt come along here in a moment with a suggestion for a module that does more closely what you desire, but I wouldn't discount the simplest approach, unless your program will have other memory intensive techniques that cannot be avoided.

      ...All the world looks like -well- all the world, when your hammer is Perl.
      ---v

        Thanks agentv,
        I will keep an open mind on this. I will be using this as the backend of a Flash game and since that will mean repeatedly incurring the memory penalty I doubt it will be the way I go. Guess it would have been much smarter of me to have said so in my original post, eh?
        Thanks for the replies!
        jg