Based on my understanding, here are two possible methods. The first one uses composite keys to store values in a single level hash. The second one uses a multi-level hash:
Method 1:
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw( sum );
my %data;
while ( my $line = <DATA> ) {
chomp $line;
my @parts = split /, /, $line;
last unless @parts;
my $value = pop @parts;
push @{ $data{$_} }, $value for @parts;
}
for my $col ( sort keys %data ) {
printf("%12s:%9d\n", $col, sum @{ $data{$col} } );
}
__DATA__
3=5002, 0=10002, 5=1, 4=1, 7=1, 8=1, 9=0, 1=14002, 6=5, 200
3=5002, 0=10002, 5=0, 4=1, 7=0, 8=0, 9=1, 1=14002, 6=5, 300
3=5001, 0=10001, 5=0, 4=0, 7=0, 8=0, 9=0, 1=14001, 6=3, 1000
3=5001, 0=10004, 5=1, 4=1, 7=2, 8=2, 9=1, 1=14001, 6=3, 10000
3=5003, 0=10004, 5=2, 4=0, 7=2, 8=2, 9=1, 1=14003, 6=8, 5000
3=5003, 0=10004, 5=3, 4=1, 7=2, 8=1, 9=0, 1=14003, 6=8, 1000
C:\Temp> hj
3=5001: 11000
3=5002: 500
3=5003: 6000
0=10001: 1000
0=10002: 500
0=10004: 16000
1=14001: 11000
1=14002: 500
1=14003: 6000
4=0: 6000
4=1: 11500
5=0: 1300
5=1: 10200
5=2: 5000
5=3: 1000
6=3: 11000
6=5: 500
6=8: 6000
7=0: 1300
7=1: 200
7=2: 16000
8=0: 1300
8=1: 1200
8=2: 15000
9=0: 2200
9=1: 15300
Method: 2
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw( sum );
my %data;
while ( my $line = <DATA> ) {
chomp $line;
my @parts = split /, /, $line;
last unless @parts;
my $value = $parts[-1];
for ( my $i = 0 ; $i < @parts - 2; ++$i ) {
my @subparts = split /=/, $parts[$i];
push @{ $data{$subparts[0]}->{$subparts[1]} }, $value;
}
}
for my $k1 ( keys %data ) {
for my $k2 ( keys %{ $data{$k1} } ) {
printf(
"%2d:%6d:%9d \n",
$k1, $k2, sum @{ $data{$k1}->{$k2} }
);
}
}
__DATA__
3=5002, 0=10002, 5=1, 4=1, 7=1, 8=1, 9=0, 1=14002, 6=5, 200
3=5002, 0=10002, 5=0, 4=1, 7=0, 8=0, 9=1, 1=14002, 6=5, 300
3=5001, 0=10001, 5=0, 4=0, 7=0, 8=0, 9=0, 1=14001, 6=3, 1000
3=5001, 0=10004, 5=1, 4=1, 7=2, 8=2, 9=1, 1=14001, 6=3, 10000
3=5003, 0=10004, 5=2, 4=0, 7=2, 8=2, 9=1, 1=14003, 6=8, 5000
3=5003, 0=10004, 5=3, 4=1, 7=2, 8=1, 9=0, 1=14003, 6=8, 1000
C:\Temp> hjk
3: 5003: 6000
3: 5002: 500
3: 5001: 11000
7: 1: 200
7: 0: 1300
7: 2: 16000
9: 1: 15300
9: 0: 2200
8: 1: 1200
8: 0: 1300
8: 2: 15000
4: 1: 11500
4: 0: 6000
1: 14001: 11000
1: 14003: 6000
1: 14002: 500
0: 10001: 1000
0: 10004: 16000
0: 10002: 500
5: 1: 10200
5: 3: 1000
5: 0: 1300
5: 2: 5000
NB: Add sort
to taste.