Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: reset of match variables

by JavaFan (Canon)
on Oct 03, 2011 at 15:09 UTC ( #929358=note: print w/replies, xml ) Need Help??

in reply to reset of match variables

$SUM += /$regex/ && $1; # Don't turn on warnings.

Replies are listed 'Best First'.
Re^2: reset of match variables
by AnomalousMonk (Chancellor) on Oct 03, 2011 at 22:25 UTC

    With warnings and strictures:

    >perl -wMstrict -lne "$::SUM += /(\d+)/ && $1; END { print $::SUM } " foo123bar foo xxx111ccc xxx ^Z 234
      Jeezes, who would want to use strict and a workaround for a one-liner??

        I write 550 character long one-liners (anything above that I convert to a script file), and I do often use strict in them.

Re^2: reset of match variables
by hbm (Hermit) on Oct 03, 2011 at 18:22 UTC

    Or even just this:

    $SUM += /$regex/;

    Update: Sorry, mine only counts the number of matches (like grep -c). To accumulate the values of matches, I still instinctively reach for (something like) this:

    nawk '/([0-9]*)/{tot+=$1}END{print tot}' file
Re^2: reset of match variables
by bart (Canon) on Oct 04, 2011 at 11:35 UTC
    Warnings do nothing: if the regexp fails then it'll return false, which is 0 when treated as a number, not undef, and without a warning.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929358]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-06-20 16:17 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.