Fwd: Error in Perlfaq6
[email protected] (Meir Michanie) Wed, 17 Aug 2011 10:56:37 +0100
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Message-ID | <CAG4bpDKdgMP7ivmoLc-eGHkL1XgnapbeDH7Utr-tGR4dWZxZGQ@mail.gmail.com> |
---------- Forwarded message ---------- From: Meir Michanie <[email protected]> Date: Wed, Aug 17, 2011 at 10:53 AM Subject: Re: Error in Perlfaq6 To: [email protected] =A0perlfaq6 - Regular Expressions ($Revision: 1.38 $, $Date: 2005/12/31 00:54:37 $) On Wed, Aug 17, 2011 at 10:52 AM, Meir Michanie <[email protected]> wro= te: > Hi Brian, > The foreach loop should have the following line of code instead: > if (/\b$pattern\b/i){print ; next LINE;} > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D > How do I efficiently match many regular expressions at once? > > =A0 =A0 =A0 ( contributed by brian d foy ) > > =A0 =A0 =A0 Avoid asking Perl to compile a regular expression every time > you want to match it. =A0In this example, perl must recompile the > regular expression > =A0 =A0 =A0 for every iteration of the foreach() loop since it has no way > to know what $pattern will be. > > =A0 =A0 =A0 =A0 =A0 @patterns =3D qw( foo bar baz ); > > =A0 =A0 =A0 =A0 =A0 LINE: while( <> ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 foreach $pattern ( @patterns = ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print if /\b$pattern\b/i; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 next LINE; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 } > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 } > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D >