luckysing has asked for the wisdom of the Perl Monks concerning the following question:
DATA: A,10 A,11 A,12 A,13 B,15 B,16 C,17 D,18
#!/usr/bin/perl use strict; use Data::Dumper; open DATA,"read"; my %sum; while (<DATA>) { next if ( /^ID/ || /^\s$/ ); my ($id, $value) = split( "," ); $sum{$id} = $sum{$id} + $value; } print Dumper \%sum;
output: $VAR1 = { 'A' => 46, 'D' => '18', 'C' => '17', 'B' => 31 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: get the average of hash values?
by toolic (Bishop) on Jul 27, 2010 at 23:29 UTC | |
by BioLion (Curate) on Jul 28, 2010 at 12:52 UTC | |
|
Re: get the average of hash values?
by Your Mother (Archbishop) on Jul 27, 2010 at 23:36 UTC | |
|
Re: get the average of hash values?
by JavaFan (Canon) on Jul 27, 2010 at 23:50 UTC | |
|