Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <p><strong>Method 1:</strong></p> <pre><code>#!/usr/bin/perl use strict; use warnings; use List::Util qw( sum ); my %data; while ( my $line = &lt;DATA&gt; ) { 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&gt; 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 </code></pre> <p><strong>Method: 2</strong></p> <pre><code>#!/usr/bin/perl use strict; use warnings; use List::Util qw( sum ); my %data; while ( my $line = &lt;DATA&gt; ) { chomp $line; my @parts = split /, /, $line; last unless @parts; my $value = $parts[-1]; for ( my $i = 0 ; $i &lt; @parts - 2; ++$i ) { my @subparts = split /=/, $parts[$i]; push @{ $data{$subparts[0]}-&gt;{$subparts[1]} }, $value; } } for my $k1 ( keys %data ) { for my $k2 ( keys %{ $data{$k1} } ) { printf( "%2d:%6d:%9d \n", $k1, $k2, sum @{ $data{$k1}-&gt;{$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&gt; 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 </code></pre> <p><strong>NB:</strong> Add <code>sort</code> to taste.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload