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

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.

Comment on Re: reset of match variables
Download Code
Replies are listed 'Best First'.
Re^2: reset of match variables
by AnomalousMonk (Canon) 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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2016-02-15 00:13 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (471 votes), past polls