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