Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

Untested:

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


Comment on Re: reset of match variables
Download Code
Replies are listed 'Best First'.
Re^2: reset of match variables
by AnomalousMonk (Abbot) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2015-07-28 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (259 votes), past polls