Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^3: searching in large file

by Cristoforo (Curate)
on Jan 07, 2018 at 23:37 UTC ( #1206868=note: print w/replies, xml ) Need Help??

in reply to Re^2: searching in large file
in thread searching in large file

Hello sabas

To answer your questions.

  1. yes
  2. In Perl, these metacharacters need to be escaped if they are to be matched literally \ | ( ) [ { ^ $ * + ? . (also called the dirty dozen) By using \Q ... \E, you escape any possible metacharacters in the variable being used for the regular expression - in this case $_
  3. He is compiling the regular expression $search_expression From Regexp Quote Like Operators the reason is Precompilation of the pattern into an internal representation at the moment of qr() avoids the need to recompile the pattern every time a match /$pat/ is attempted So, this avoids compiling the regular expression each time it is encountered in the while loop below next unless m/$search_expression/;
  4. Go to the top of the while loop and get the next line unless the regular expression matches this line. This skips the lines of code below if true.

Replies are listed 'Best First'.
Re^4: searching in large file
by NetWallah (Canon) on Jan 08, 2018 at 03:32 UTC
    Thank you, Cristoforo (++) for your excellent explanation.

    I wanted to add that for point (2), in addition to escaping the content($_) (with \Q and \E), the line appends the alternation meta-chaacter (|).

    This makes the regular expression search for ANY of the lines in the smallfile.
    Note also the "chop" statement which deletes the extra "|" appended.

                    We're living in a golden age. All you need is gold. -- D.W. Robertson.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-04-15 13:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found