Perl - Summarize Data in File

Whats the best way to summarize data from a file that has around 2 million records in Perl?

For eg: A file like this,

ABC|XYZ|DEF|EGH|100

ABC|XYZ|DEF|FGH|200

SDF|GHT|WWW|RTY|1000

SDF|GHT|WWW|TYU|2000

Needs to be summarized on the first 3 columns like this,

ABC|XYZ|DEF|300

SDF|GHT|WWW|3000

Chris

Answers


Assuming there are always five columns, the fifth of which is numeric, and you always want the first three columns to be the key...

use warnings;
use strict;

my %totals_hash;

while (<>)
{
  chomp;
  my @cols = split /\|/;

  my $key = join '|', @cols[0..2];

  $totals_hash{$key} += $cols[4];
}

foreach (sort keys %totals_hash)
{
  print $_, '|', $totals_hash{$_}, "\n";
}

Need Your Help

Getting information about an error with EventSource

node.js error-handling event-stream etw-eventsource

I'm trying to get information about an error when using EventSource in NodeJs, I think you could understand me better with the following example:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.