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

saving matching pattern with flip-flop operator

by raja_infy (Initiate)
on Jun 23, 2011 at 10:23 UTC ( [id://911044]=perlquestion: print w/replies, xml ) Need Help??

raja_infy has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a question on flip-flop operator. I have a script something like this

while (<>) { if(/START/.../END TIME (.+)/) { next if(/START/ | /^\s*$/); print; } }

The flop string 'END TIME (.+)' has a variable time at the end which I would like to extract and print. I see that the script match the 'END TIME (.+)', returns false and returns to the while loop beginning, but the matched sub-pattern (.+) is not stored anywhere ($1, $&, $+ etc). How do I extract this ?

Thanks in advance

-Raja

Replies are listed 'Best First'.
Re: saving matching pattern with flip-flop operator
by bart (Canon) on Jun 23, 2011 at 10:54 UTC
    You could use $1, but I expect problems with retained old values if the regex didn't match.

    So I prefer to directly, explicitly capture the captured value. Like this:

    while (<>) { if(/START/ ... (my($time) = /END TIME (.+)/)) { unless(defined $time) { next if /START/ || /^\s*$/; print; } else { print "End time at $time\n"; } } }
    BTW | is for bitwise or; you (ought to) want either or or ||.
Re: saving matching pattern with flip-flop operator
by wfsp (Abbot) on Jun 23, 2011 at 10:35 UTC
    The second regex clears the capture from the first.
    while (<DATA>) { if(/START/.../END TIME (.+)/) { my $time = $1; next if(/START/ | /^\s*$/); print $time, qq{\n}; } } __DATA__ START END TIME 123 START END TIME 456
    123 456
Re: saving matching pattern with flip-flop operator
by planetscape (Chancellor) on Jun 23, 2011 at 12:26 UTC
Re: saving matching pattern with flip-flop operator
by Anonymous Monk on Jun 23, 2011 at 10:38 UTC
    but the matched sub-pattern (.+) is not stored anywhere

    Yes it is

    #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { for my $outer ( 1 .. 3 ) { printf qq[%5s %5s %5s %5s \n], qw' out inn flip $1 '; for my $inner (1..10){ my $flip = $inner =~ /2/ .. $inner =~ /(5)/ ; printf qq[%5s %5s %5s %5s \n], $outer, $inner, $flip, $1 | +|''; } print "\n"; } } __END__ out inn flip $1 1 1 1 2 1 1 3 2 1 4 3 1 5 4E0 5 1 6 5 1 7 5 1 8 5 1 9 5 1 10 5 out inn flip $1 2 1 2 2 1 2 3 2 2 4 3 2 5 4E0 5 2 6 5 2 7 5 2 8 5 2 9 5 2 10 5 out inn flip $1 3 1 3 2 1 3 3 2 3 4 3 3 5 4E0 5 3 6 5 3 7 5 3 8 5 3 9 5 3 10 5
Re: saving matching pattern with flip-flop operator
by choroba (Cardinal) on Jun 23, 2011 at 10:39 UTC
    Do you really want to use binary OR in the next if condition?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://911044]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found