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

Sum of values in an array

by Anonymous Monk
on Sep 25, 2014 at 19:34 UTC ( [id://1102027]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I would like to have a total sum of the values from this array of hashes, any suggestions?
... print Dumper \@pe_total; $VAR1 = [ { 'COMP_TOTAL' => '2445.00' }, { 'COMP_TOTAL' => '728.00' } ]; ...

Thanks for help!

Replies are listed 'Best First'.
Re: Sum of values in an array
by toolic (Bishop) on Sep 25, 2014 at 19:40 UTC
    One way:
    use warnings; use strict; use List::Util qw(sum); my @pe_total = ( { 'COMP_TOTAL' => '2445.00' }, { 'COMP_TOTAL' => '728.00' } ); print sum(map { $_->{COMP_TOTAL} } @pe_total), "\n"; __END__ 3173

    See also:

      Would it be possible to be done without loading a Perl Module?

        List::Util is in the Perl core. Try it out, you should already have it installed.

        Otherwise, BrowserUk has given you an alternative method.

Re: Sum of values in an array
by BrowserUk (Patriarch) on Sep 25, 2014 at 19:52 UTC

    Updated to address GrandFather's concern below.

    my @pe_total = ( { 'COMP_TOTAL' => '2445.00' }, { 'COMP_TOTAL' => '728.00' } );; my $total; $total += $_->{COMP_TOTAL} for @pe_total; print $total;; 3173

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I was surprised by my $total += $_->{COMP_TOTAL} for @pe_total; so I ran the code. No output generated so I modified the print to print "$^V >$total<"; which prints:

      v5.16.3 ><

      What is different about the way you are running the sample code? Different Perl verion maybe?

      The B::Deparse of the summing line is:

      (my $total += $$_{'COMP_TOTAL'}) foreach (@pe_total);

      but my Perl foo isn't strong enough to decide if that means $total is local to the loop body or not. The result implies that it is, at least for this version of Perl.

      Perl is the programming world's equivalent of English

        You're right. I ran it in my REPL, thus globals; and added the my when posting (to encourage its use). Now corrected above. Thanks.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102027]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found