Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

fastest pattern match

by Anonymous Monk
on Jun 24, 2002 at 19:28 UTC ( #176908=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am a new bie.. I have a file having 50,000 lines. I want to match a pattern. I read the whole file in an array and then match the regex. I want only the line with the first match. the code is something like :-
open( FILE, "file" ) || die ("The file not there "); #open the file with 50,000 lines (some 1MB memory) @list=<FILE>; # put this in an array. close(DICTIONARY); # close the file $regex="somebla.."; #this is the regex to be matched @match=grep(/$regex/,@list); #the above line returns an array of all lines matched # but I want only first one !! Pity don't know how to # get only that.. print $match[0]; #so I get the first match of the retrned array.. #that is poor .grep has to go through the complet list # although I need the first match only
I request the help from perl monks.. With lot of thanks!! Pandey

Comment on fastest pattern match
Download Code
Re: fastest pattern match
by neilwatson (Curate) on Jun 24, 2002 at 19:35 UTC
    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

      You should use m/$regex/o or the regex will be compiled every iteration, slowing things down considerably



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

        Update: Fastolfe's bad...mea culpa

        ~monk d4vis

        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:??;

        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.


Re: fastest pattern match
by lestrrat (Deacon) on Jun 24, 2002 at 19:38 UTC


    #!/usr/local/bin/perl use strict; open( FILE, "file" ) || die $!; while( <FILE> ) { if( /regexp/ ) { print; last; } }

    or better yet ( also untested )

    perl -ne 'print, last if /regexp/' file
      last; that's the command I was trying to remember. Not exit. My head is filled with to much useless information.

      Neil Watson

Re: fastest pattern match
by Cody Pendant (Prior) on Jun 25, 2002 at 04:05 UTC
    Just want to note that if this is your actual code:
    open( FILE, "file" ) || die ("The file not there "); [snip] close(DICTIONARY); # close the file
    you're opening one file, then closing another...
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://176908]
Approved by jlk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-06-29 23:43 GMT
Find Nodes?
    Voting Booth?

    What kind of chocolate gives you the most pleasure?

    Results (774 votes), past polls