Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Text matching repost

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

in reply to Text matching repost

How about a different approach?
#!/usr/bin/perl use strict; use warnings; my ($start, $stop) = (qr '#ifdef|#if defined', qr '#endif'); open INPUT, '<', 'monks23.dat' or die "Error opening input: " . $!; while (<INPUT>) { if (/$start/ .. /$stop/) { # Print the lines themselves if (/$start/ or /$stop/) { print; } # Skip the rest next; } print; } close INPUT;
Glad I've finally found a useful non-numeric-iterating purpose for .. :)
Edit: Added comments, removed unnecessary regex escaping

Replies are listed 'Best First'.
Re^2: Text matching repost
by Neighbour (Friar) on Jun 18, 2012 at 12:19 UTC
    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;
      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" }'


      0 matched zero


      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 :)
Re^2: Text matching repost
by prassi (Acolyte) on Jun 18, 2012 at 10:45 UTC

    What kind of regex is this the above you mentioned, can you point me to some document on more understanding on this.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976798]
marto has a similar idea on the 'to do' list
[Discipulus]: 65-35 joint venture marto? ;=)
[choroba]: Fibonacci at 34!
[marto]: Discipulus, what did you have in mind?
[marto]: sort of related, have you seen sonic pi
[Discipulus]: choroba less than perl -le ' $==1,(1 x $_)=~/(^)(1|11\1 )*(?{$=++})^/, print $= for 0..10'?? congrats

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-10-17 10:01 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (225 votes). Check out past polls.