in reply to Re^2: adding values of element
in thread adding values of element
Just read each line, remove the line terminator (chomp) then split name and value and add value to the appropriate hash element.
$ perl -Mstrict -Mwarnings -MData::Dumper -E ' open my $dataFH, q{<}, \ <<EOF or die $!; john, 100 barry, 300 john, 200 mary, 150 barry, 200 EOF my %names; while ( <$dataFH> ) { chomp; my( $name, $value ) = split m{\s*,\s*}; $names{ $name } += $value; } print Data::Dumper->Dumpxs( [ \ %names ], [ qw{ *names } ] );' %names = ( 'john' => 300, 'barry' => 500, 'mary' => 150 ); $
I hope this is helpful but ask further if you need more explanation.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: adding values of element
by Anonymous Monk on Jan 22, 2015 at 00:35 UTC | |
|
Re^4: adding values of element
by new2perl2 (Initiate) on Jan 22, 2015 at 00:16 UTC |