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
|
|---|