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

Re^3: Adding numbers from a loop

by stevieb (Canon)
on Aug 07, 2019 at 00:03 UTC ( [id://11104070]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Adding numbers from a loop
in thread Adding numbers from a loop

Are you looking to gather aggregates based on whether a total is something?

For instance, you appear to be looping over db rows, so if 'total' column is '3', you want to sum those up individually?

If so, use a hash:

my %h; ...; # get info from db $h{$total} += $revenue;

Example:

use warnings; use strict; use Data::Dumper; my %h; while (<DATA>){ my ($total, $revenue) = split; $h{$total} += $revenue; } print Dumper \%h; __DATA__ 3 555 4 962 3 1 3 1064 5 19 17 8 45 -1

Output:

$VAR1 = { '5' => 19, '3' => 1620, '45' => -1, '4' => 962, '17' => 8 };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found