Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Add numbers row by row

by davido (Cardinal)
on Oct 25, 2013 at 16:31 UTC ( #1059695=note: print w/replies, xml ) Need Help??


in reply to Add numbers row by row

I don't think that toolic's approach could be improved upon, but that doesn't mean we shouldn't be able to have a little fun. ;)

use feature qw(state); tally(\*DATA); sub tally { my $fh = shift; state $acc = 0; return if ! defined( local $_ = <$fh> ); $acc += ( my( $n, $d ) = split )[0]; printf "%-5d %s\n", $acc, $d; tally($fh); } __DATA__ 1 2005-01-16 6 2005-01-28 1 2005-01-31 2 2005-02-01 2 2005-02-02 1 2005-02-05

One possible "improvement" to this one: Tie $n to a class that increments upon STORE, and you can eliminate state $acc, just using $n as the accumulator. :)


Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-01-19 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?