Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: fastest pattern match

by neilwatson (Priest)
on Jun 24, 2002 at 19:35 UTC ( #176912=note: print w/replies, xml ) Need Help??

in reply to fastest pattern match

Read the file one line at a time. When you hit your match exit:

open( FILE, "file" ) || die ("The file not there "); while (<>){ if (m/$regex/) { print "matched!\n"; close (FILE); exit; } }

Neil Watson

Replies are listed 'Best First'.
Re^2: fastest pattern match
by flounder99 (Friar) on Jun 24, 2002 at 19:50 UTC
    You should use m/$regex/o or the regex will be compiled every iteration, slowing things down considerably



      Incorrect. It will not be compiled on every iteration, but its string form will be compared with its previous string form, to see whether it needs recompiling.

      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


        I remember reading that it still did the recompile even if the variable in the regex did not change. Of course I read that a LONG time ago. Apparently it has been fixed.(probably a long time ago) So would the /o faster by avoiding the extra string compare? I know this would probably be insignificant unless there where a bajillion iterations. But I was just curious.
        Do you even know anything about regexes? (see sig)



        Do you even know anything about perl?
        --Anonymous Coward to Tom Christiansen on slashdot

      That's JAN-AR-IK..(like generic) Just another Regexp Question.

      Also it might be worth mentioning that meta characters in $regex will still be parsed. Make sure you intended that to happen.


      Brother Frankus.


      Good advice in general,
      but in this case there'll only be one interation.

      Update: Fastolfe's bad...mea culpa

      ~monk d4vis

        Why? If we were sure the match would always occur on the first line, why search for it at all?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://176912]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2016-10-25 08:55 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (315 votes). Check out past polls.