Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

fastest pattern match

by Anonymous Monk
on Jun 24, 2002 at 19:28 UTC ( [id://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

Replies are listed 'Best First'.
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 neilwatson (Priest) 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



        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.


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

        Update: Fastolfe's bad...mea culpa

        ~monk d4vis

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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://176908]
Approved by jlk
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-13 03:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found