If there are numerous lookups, I agree, but if it's just a couple of quick lookups, that's not necessarily true. To extract the string into a hash seems to take significantly longer than just a straight regex lookup:

#!/usr/bin/perl use warnings; use strict; use Benchmark qw(:all); cmpthese ( 5000000, { 'string' => 'string()', 'hash' => 'hash()', }); sub string { my $str = "1689 9679 2792 2514 5472 1520 9342 5544 1268 0165 1979 +7314 2101 7221 9539 3882 1812"; my $test = 2101; if ($str =~ $test){ # do stuff } } sub hash { my $str = "1689 9679 2792 2514 5472 1520 9342 5544 1268 0165 1979 +7314 2101 7221 9539 3882 1812"; my $test = 2101; my %hash; for (split /\s+/, $str){ $hash{$_}++; } if (exists $hash{$test}){ # do stuff } } __END__ Rate hash string hash 267237/s -- -92% string 3521127/s 1218% --

In reply to Re^2: Regex with variables by stevieb
in thread Regex with variables by cormanaz

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.