Actually compared to the translate (counting) technique, for a case sensitive match McDarren's solution using the equivelent regex technique is slightly faster, but it's slightly slower if using a case insensitive match. Case sensitivity makes no significant difference to the translate.

For the sake of code clarity I'd actually go with the multiple regex solution, but offered the translate solution because the technique gets forgotten about somewhat.

use strict; use warnings; use Benchmark qw(cmpthese); my @words = qw(perl purile pretty reputation reputable longwithoutanym +atch); cmpthese (-1, { tr => \&do_tr, tri => \&do_tri, m => \&do_m, mi => \&do_mi, } ); sub do_tr { my $matched = 0; $matched += tr/p// && tr/e// && tr/r// && tr/l// for @words; return $matched; } sub do_tri { my $matched = 0; $matched += tr/pP// && tr/eE// && tr/rR// && tr/lL// for @words; return $matched; } sub do_m { my $matched = 0; $matched += m/p/i && /e/i && /r/i && /l/i for @words; return $matched; } sub do_mi { my $matched = 0; $matched += m/p/ && /e/ && /r/ && /l/ for @words; return $matched; } --------- 8< -------------------- Rate m tri tr mi m 98443/s -- -15% -16% -22% tri 115597/s 17% -- -1% -9% tr 116736/s 19% 1% -- -8% mi 126385/s 28% 9% 8% --

DWIM is Perl's answer to Gödel

In reply to Re^3: A Quick Regex Question by GrandFather
in thread A Quick Regex Question by chinamox

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.