I have a flat file which is pipe delimited. Each line has two values. I'm attempting to show unique values for the first field in each line and, where there are duplicates, add the values provided in the second field. The following code is what I have so far:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @count; my ($field1, $field2); while (<DATA>) { ($field1, $field2) = split/\|/; push @count, {'field1' => $field1, 'field2' => $field2}; } print Data::Dumper->Dump([\@count]); __DATA__ 10|10 20|20 30|30 10|100 15|15 50|50 15|150
I believe what I need is to have data dumper output something as follows:
$VAR1 = { '10' => [ '10', '100' ], '20' => [ '20' ], '30' => [ '30', ], '15' => [ '15', '150' ], '50' => [ '50' ] };
By this, I should be able to loop through and add the values of the field 2 keys to give me the sum I'm looking for. This is new territory for me so any suggestions are appreciated. cheers.
In reply to working with a hash by semio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |