Perhaps the following will be helpful:

use strict; use warnings; my $base = 0; my @cols = qw/2 3/; my %hash; while ( my $line = <DATA> ) { my @vals = split ' ', $line; $hash{ $vals[$base] }[$_] += $vals[ $cols[$_] ] for 0 .. $#cols; $hash{ $vals[$base] }[ $#cols + 1 ]++; } for my $key ( sort keys %hash ) { print "$key"; print "\t$hash{$key}[ $#cols + 1 ]\t$hash{$key}[$_]" for 0 .. $#co +ls; print "\n"; } __DATA__ U1 ID1 100 280 30 350 U1 ID1 137 250 70 200 U2 ID2 150 375 400 50 U1 ID2 100 100 100 375 U3 ID1 100 600 50 100 U9 ID3 137 200 75 150

Output when $col = 0; @cols = qw/2 3/:

U1 3 337 3 630 U2 1 150 1 375 U3 1 100 1 600 U9 1 137 1 200

Output when $col = 1; @cols = qw/2 4 5/:

ID1 3 337 3 150 3 650 ID2 2 250 2 500 2 425 ID3 1 137 1 75 1 150

The while loop builds a data structure like the following, which is then later printed in the for loop:

$VAR1 = { 'U1' => [ 337, 630, 3 ], 'U2' => [ 150, 375, 1 ], 'U9' => [ 137, 200, 1 ], 'U3' => [ 100, 600, 1 ] };

Edit: Updated to more closely meet the OP's scalability specs.


In reply to Re: Perl Sum & Count column based data by Kenosis
in thread Perl Sum & Count column based data by ler224

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.