in reply to get the average of hash values?
use strict; use warnings; my %data; while (<DATA>) { chomp; my ($id, $value) = split /,/; $data{$id}{sum} += $value; $data{$id}{count}++; } for my $id (sort keys %data) { my $avg = $data{$id}{sum}/$data{$id}{count}; print "$id: sum=$data{$id}{sum} avg=$avg\n"; } __DATA__ A,10 A,11 A,12 A,13 B,15 B,16 C,17 D,18
prints:
A: sum=46 avg=11.5 B: sum=31 avg=15.5 C: sum=17 avg=17 D: sum=18 avg=18
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: get the average of hash values?
by BioLion (Curate) on Jul 28, 2010 at 12:52 UTC |