That's basically the way how I would have done it myself, so no bad word from me there. :] Well, some remarks:
  1. I think this likely should only try to match whole words, so it won't try to match "tom" inside "stomach". For that, add '\b' anchors:
    /\b($regexp)\b/
    with whatever modifiers you like. If this regexp won't ever change, I'd add the /o modifier.
  2. If the "words" can contain special characters, the words should be quotemeta'ed before being added to the regexp:
    my $regexp = join('|', map quotemeta, keys %temphash);
  3. If the number of words to match can get big, it might be worthy to find a way to construct a cleverer regexp from this wordlist. There's a useful module on CPAN that does just that: Regex::PreSuf. To quote the description from the docs:
    The presuf() subroutine builds regular expressions out of 'word lists', lists of strings. The regular expression matches the same words as the word list. These regular expressions normally run faster than a simple-minded '|'-concatenation of the words.
    The larger the wordlist, the higher the gain likely will be.

In reply to Re: Re: how to find hash keys in a string ? by bart
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.