The most idiomatic and most easily-understandable solution for "does a string contain any of these items" is:

    if ( grep { $str =~ /$_/ } @arr ) { ... }

I ran the test again, with this new strategy added and a few other changes to the code:

Also, there is a small but important bug in your code:

  first BLOCK, ARRAY

should be:

  first BLOCK ARRAY

The former will only run the BLOCK once, regardless of outcome, and throw away the result. (Oh Perl syntax, why must you be so baroque?)

So how does the new method fare in this adjusted test?

word_altn 506/s grep_block_index 2356/s was: block_grep grep_expr_index 2373/s was: expr_grep grep_block_regex 3957/s <-- new method here first_index 4179/s was: list_util_index first_regex 6871/s was: list_util_regex
The pleasant surprise is that a plain grep-regex solution turns out to be faster than replacing the regex with an index() call. Replacing grep() with first() gives a further speedup of about 75%, but as we've just seen you lose some maintainability, and that can be important.

New code:

#!/bin/perl use strict; use warnings; use List::Util qw( first ); use Benchmark qw( cmpthese timethese ); my @arr = qw( foo bar baz qux cool foo bar baz qux ); my $manywords = 'TONS OF WORDS ' x 500; my $str = $manywords . 'I am cool ' . $manywords; my @invocation; my $results = timethese( 0, { 'word_altn' => sub { local $" = '|'; if( $str =~ /@arr/ ) { ++$invocation[0]; # print "Matched\n" } }, 'grep_block_index' => sub { if( grep { index($str, $_) !=-1 } @arr) { ++$invocation[1]; # print "Matched\n" } }, 'grep_expr_index' => sub { if( grep index($str, $_) !=-1, @arr) { ++$invocation[2]; # print "Matched\n" } }, 'first_index' => sub { if( first { index($str, $_) != -1 } @arr ) { ++$invocation[3]; #print "Matched\n" } }, 'first_regex' => sub { if( first { $str =~ /$_/ } @arr ) { ++$invocation[4]; #print "Matched\n" } }, 'grep_block_regex' => sub { if( grep { $str =~ /$_/ } @arr ) { ++$invocation[5]; #print "Matched\n" } }, }, 'none', ); print "@invocation\n"; die unless grep { $_>0 } @invocation == @invocation; cmpthese $results;

In reply to Re^2: Mathching an array in regex by mrpeabody
in thread Mathching an array in regex by narainhere

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.