Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Add numbers row by row

by epimenidecretese (Acolyte)
on Oct 25, 2013 at 14:01 UTC ( #1059670=perlquestion: print w/replies, xml ) Need Help??

epimenidecretese has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to add numbers row by row but I can't figure out how to do it.

example:

__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

I'd like to have an output like this:

1 2005-01-16 7 2005-01-28 8 2005-01-31 10 2005-02-01 12 2005-02-02 13 2005-02-05

any tipps?

One of Crete's own prophets has said it: 'Cretans are always liars, evil brutes, lazy gluttons'.
He has surely told the truth.

Replies are listed 'Best First'.
Re: Add numbers row by row
by toolic (Bishop) on Oct 25, 2013 at 14:05 UTC
    use warnings; use strict; my $tot = 0; while (<DATA>) { my ($num, $date) = split; $tot += $num; printf "%-5d %s\n", $tot, $date; } __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
      Thank you very much! Works great. Where could I learn something about this string?
      printf "%-5d %s\n", $tot, $date;
      I don't really understand what's going on.

      One of Crete's own prophets has said it: 'Cretans are always liars, evil brutes, lazy gluttons'.
      He has surely told the truth.

        You're welcome.
        Where could I learn something about this string?

        In a variety of places:

        • The "Function Nodelet", which is on the right side near the bottom of the page of my browser here at the Monastery, has a link to the printf docs. I'm not sure if this nodelet is enabled by default.
        • The official documentation is online: printf
        • And it is also at your command prompt: perldoc -f printf
        > Where could I learn something about this string?

        You either type "perldoc -f printf" or sprintf into your console ...

        ... or you check the online docs at http://perldoc.perl.org (but beware of the Perl version)

        It's also searchable per "duckduckgo: perl printf" or "google: perl printf" or other search machines...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Add numbers row by row
by davido (Cardinal) on Oct 25, 2013 at 16:31 UTC

    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: perlquestion [id://1059670]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2019-12-14 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?