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


In reply to Re: Re: how to find hash keys in a string ? by Limbic~Region
in thread how to find hash keys in a string ? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.