in reply to Re: how to find hash keys in a string ?
in thread how to find hash keys in a string ?

Dr. Mu,
Ok - I admit defeat. This method is certainly faster than mine. There are a couple of things I would like to point out though.

You have chosen to work of words in the string rather than the keys of the hash. If someone were really doing this type of analysis on a large data set, it might be worthwhile to have multiple methods and choose the best one based on the data set. For instance - imagine that there are only 3 hash keys, but the string is 1,500 words long. It certainly doesn't make sense to use this method.

You have also assumed that the hash keys will not contain any spaces. My solution allows for this, but still has the requirement of having a word border on both ends. For insance:

my $temphash{'good boy'} = "blah"; my $string = "He was a good boy when we went to the store"; my $otherstring = "He is worthless, not a good boyfriend at all";
Your method will not work in either case. My method will correctly match in $string but will correctly fail in $otherstring.
Of course, knowing your data is what counts so that you can code for it.

My hat is off to you for such an innovative solution.

Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Re: how to find hash keys in a string ?
by Dr. Mu (Hermit) on May 04, 2003 at 07:59 UTC
    All good points! -- except the admitting defeat part. ;-) The ever-unfolding wonder of PM is the diversity of approaches to any given problem and the discussions that they stimulate. We all win as a consequence. Frankly, I didn't really consider my method to be that innovative, hoping instead to employ (and learn about) hash slices in lieu of the grep -- but to no avail. Nonetheless, what resulted was different enough, and it worked -- subject to the very real limitations you point out. Thanks and ++ to BrowserUK for troubling to do the efficiency analysis that I was too lazy to perform!