Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

unexpected RE match

by WoodyWeaver (Monk)
on Jul 05, 2017 at 22:54 UTC ( [id://1194316]=perlquestion: print w/replies, xml ) Need Help??

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

I'm parsing time from a last output. I got an unexpected input, so getting an unexpected output shouldn't be surprising, but for the life of me I can't figure out what perl is doing.

subroutine is

sub minutes { my $time = shift; print "minutes handed $time " if $DEBUG_MINUTES; if ( $time =~ /(\d+):(\d+)/ ) { print "Interpreted as $1 hours $2 mins\n" if $DEBUG_MINUTES; return 60 * $1 + $2; } elsif (/(\d+)+(\d+):(\d+)/) { print "Interpreted as $1 days, $2 hours, $3 minutes\n" if $DEBUG_M +INUTES; return ( 24 * $1 + $2 ) * 60 + $3; } else { warn "I don't understand time $time\n"; return $time; } }
I was processing a line
archerh Thu Jun 22 10:25 - down (-6+-1 +2:-52)
and I get
minutes handed -6+-12:-52 Interpreted as 1 days, 0 hours, 25 minutes
How does \d+ do this?

Replies are listed 'Best First'.
Re: unexpected RE match
by Anonymous Monk on Jul 05, 2017 at 23:01 UTC

    elsif (/(\d+)+(\d+):(\d+)/) matches against $_ because you forgot to say elsif ( $time =~ /(\d+)+(\d+):(\d+)/ ), and $_ is probably the whole input line, and

    use Data::Dumper; print Dumper( "Thu Jun 22 10:25" =~ /(\d+)+(\d+):(\d+)/ ); __END__ $VAR1 = '1'; $VAR2 = '0'; $VAR3 = '25';
      many thanks. we always see what we expect, not what is there.
        Also note that your regex isn't going to match those minus signs. -G
        PerlMonks iz awe-some. I love the vibe.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found