<?xml version="1.0" encoding="windows-1252"?>
<node id="336461" title="Re: Re: Finding dictionary words in a string." created="2004-03-14 00:58:59" updated="2005-06-13 01:10:07">
<type id="11">
note</type>
<author id="81749">
toma</author>
<data>
<field name="doctext">
This is a great little program!&lt;P&gt;
I couldn't resist speeding
it up a bit.  A commonly used trick in 
search engines is to
shift everything to upper case.&lt;P&gt;

Avoiding the &lt;CODE&gt;i&lt;/CODE&gt;
flag in the regular expression roughly doubles
the speed, on my machine at least.&lt;P&gt;

I also used Storable to create a slightly faster-loading
dictionary.  &lt;P&gt;


&lt;CODE&gt;
#!/usr/bin/perl -w
use strict;
use Storable;

my $search_word = uc shift
   or die "search word required\n";

my @words = ();
my @dict;
my $rdict;
my $count = 0;
if (not -e 'words.sto') {
  open WORDS, '/usr/share/dict/words'
    or die "can't open words file\n";
  while (&lt;WORDS&gt;) {
    chomp;
    push @dict, uc $_;
  }
  close WORDS;
  $rdict= \@dict;
  store $rdict, 'words.sto';
}
else {
  $rdict= retrieve('words.sto');
}

for (@$rdict) {
  printf "%3d %s\n", ++$count, $_ if $search_word =~ /$_/;
}

&lt;/CODE&gt;

&lt;div class="pmsig"&gt;
&lt;div class="pmsig-81749"&gt;
&lt;div class="pmsig"&gt;&lt;I&gt;It should work perfectly the first time! - toma&lt;/I&gt;&lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
336331</field>
<field name="parent_node">
336339</field>
</data>
</node>
