Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Text matching repost

by Neighbour (Friar)
on Jun 18, 2012 at 12:19 UTC ( #976821=note: print w/replies, xml ) Need Help??


in reply to Re: Text matching repost
in thread Text matching repost

I later realised that instead of using
if (/$start/ or /$stop/) { print; }
I could just as well use (and more efficient too):
if ($&) { print; }
The meaning of $& is explained in perlvar as
The string matched by the last successful pattern match.
You can also use $MATCH if you use English;

Replies are listed 'Best First'.
Re^3: Text matching repost
by afoken (Monsignor) on Jun 18, 2012 at 14:24 UTC
    I could just as well use (and more efficient too):
    if ($&) { print; }

    Ummm, no. $& is one of the "ugly three" variables (the other two are $` and $') that kill performance. From perlvar:

    The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. To avoid this penalty, you can extract the same substring by using @-. Starting with Perl 5.10, you can use the /p match flag and the ${^MATCH} variable to do the same thing for particular match operations.

    Apart from that, it may work for this special problem, but it does not work if $& evaluates to false:

    perl -E '$x="a0b"; $x=~/0/; say $&; if ($&) { die "not reached!" } if + ($x=~/0/) { say "matched zero" }'

    Output:

    0 matched zero

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Good call, it shall be
      if (scalar @-) { print; }
      then :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976821]
help
Chatterbox?
[james28909]: i know but scroll through your calendar on your computer.
[james28909]: i thiught you were going to say make both hands into a fist and puch yourself in the face
[atcroft]: .oO(Sorry, I probably should have said take two normal hands....)
[atcroft]: james28909: No, unless you are a politician, I wouldn't say that (and even if you are, I still probably wouldn't say that).
[james28909]: i mean how hard can it be? its just subtracting days lol
[atcroft]: james28909: What about October 5, 1582?
[stevieb]: atcroft: "Make both hands into fists..."... is something my Ma taught me in our native lang, but I was to ignorant and young to pay attention. Thanks for that :)
[atcroft]: stevieb: Sad to say that I only recently learned that particular trick, but I have since found it very useful.... :)
[james28909]: ill be back with a solution eventually
[stevieb]: it's a reminder to re-inforce it :P

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-04-29 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (531 votes). Check out past polls.