Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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]
[corenth]: each element was several three + element references
[corenth]: That FANN thing sounds like something I
[Cosmic37]: When I say I like LGPL what I mean is that I like the GPL aspect. I am a fan of Rishard Stallman and the Free Software Foundation but I wish I could be more virtuous... self-flagellates
[corenth]: . . . I'd like to play with
[corenth]: I'm a pragmatic free-software advocate. I'll play in a walled sandbox when I need to.
[erix]: hm, there's a FANN perl module - looks a bit old
[Cosmic37]: I'm afraid I don't know the answer to your map question...... what were the references too?
[corenth]: each reference was to a fairly small scaler.
[corenth]: I mean, array of scalers. usually triples.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-02-20 18:26 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.