in reply to Re^3: Best way to store/sum multiple-field records?
in thread Best way to store/sum multiple-field records?
I've had a look over both yours and toolics' solutions, and decided to stick with yours, as I found dealing with the output of your version a lot easier - not too sure how to get the output from toolics' version into a nice pipe-delimited format. Had a poke around on Google and here, and couldn't find anything that my mind attached itself to as a reasonable solution.
So, after having a bit more of a play, this is what I've ended up with. Could you take a look, and see if you can see anything wrong with it, please?
Prints:#!/usr/bin/perl use strict; use warnings; my %sum; while (<DATA>) { chomp; my ($key, $value, $reason) = split(/\|/); if (! defined $reason || $value !~ /^\d+$/) { warn qq<dropped line: "$_"\n>; next; } $sum{$key}{value} += $value; if (! defined $sum{$key}{reason}) { $sum{$key}{reason} = $reason; } } local $" = '/'; for my $key (keys %sum) { print "$key|$sum{$key}{value}|$sum{$key}{reason}\n"; } __DATA__ USERID1|2215|Jones,Tom| USERID1|1000|Jones, Tom| USERID3|1495|Dole, Bob| USERID2|2500|Francis, Pope| USERID2|1500|Francis, Pope| USERID4|0045|Doe, John| USERID5|1225|Doe, Jane| USERID4|4995|Doe, John| USERID4|9995|Doe, John| USERID6|1095|Darwin, Anita| USERID7|1495|Dawson, Gary| USERID6|1250|Darwin, Anita|
USERID5|1225|Doe, Jane USERID3|1495|Dole, Bob USERID7|1495|Dawson, Gary USERID1|3215|Jones,Tom USERID4|15035|Doe, John USERID2|4000|Francis, Pope USERID6|2345|Darwin, Anita
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Best way to store/sum multiple-field records?
by GrandFather (Saint) on Dec 23, 2014 at 05:26 UTC |