in reply to efficient method of matching a string against a list of substrings?

Create a hash of arrays where the keys are the first two chars of your candidate substrings and the values are the substrings that begin with those chars. e.g.,
$hash{'Ge'} = ['George', 'George Bush'];
Then you can run through your hash, checking for the presence of the keys in your string. When a key is found, you look for its values. It might be something of an optimization to remember the pos where the key was found so your value searches could start there.

Also, study might be your friend for this, if you use a regex rather than index.


Caution: Contents may have been coded under pressure.
  • Comment on Re: efficient method of matching a string against a list of substrings?
  • Download Code