develooper Front page | perl.perlfaq.workers | Postings from August 2011

Fwd: Error in Perlfaq6

From:
Meir Michanie
Date:
August 22, 2011 06:47
Subject:
Fwd: Error in Perlfaq6
Message ID:
CAG4bpDKdgMP7ivmoLc-eGHkL1XgnapbeDH7Utr-tGR4dWZxZGQ@mail.gmail.com
---------- Forwarded message ----------
From: Meir Michanie <meirgotroot@gmail.com>
Date: Wed, Aug 17, 2011 at 10:53 AM
Subject: Re: Error in Perlfaq6
To: brian.d.foy@gmail.com


 perlfaq6 - Regular Expressions ($Revision: 1.38 $, $Date: 2005/12/31
00:54:37 $)


On Wed, Aug 17, 2011 at 10:52 AM, Meir Michanie <meirgotroot@gmail.com> wrote:
> Hi Brian,
> The foreach loop should have the following line of code instead:
> if (/\b$pattern\b/i){print ; next LINE;}
>
> ==================================================
> How do I efficiently match many regular expressions at once?
>
>       ( contributed by brian d foy )
>
>       Avoid asking Perl to compile a regular expression every time
> you want to match it.  In this example, perl must recompile the
> regular expression
>       for every iteration of the foreach() loop since it has no way
> to know what $pattern will be.
>
>           @patterns = qw( foo bar baz );
>
>           LINE: while( <> )
>               {
>                       foreach $pattern ( @patterns )
>                               {
>                       print if /\b$pattern\b/i;
>                       next LINE;
>                               }
>                       }
> ===================================================
>



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About