Have a look at Data::Table and see whether it can do what you want:

use strict; use warnings; use Data::Table; use List::Util qw/sum/; my $data = [ map { chomp; [ split /\s+/ ] } <DATA> ]; # read data i +nto array of arrays my $header = [ map { "col$_" } 0..@{$data->[0]}-1 ]; # create some + headers for the data my $t = new Data::Table( $data, $header, 0 ); my $summary = $t->group( [ "col0" ], + # base column [ "col2", "col2", "col3", "col3" ], + # fields [ sub{scalar @_}, sub{sum @_}, sub{scalar @_} +, sub{sum @_}, ], # aggregation functions [ "Count2", "Sum2", "Count3", "Sum3" ], + # names of result columns 0 ); + # do not display other columns print $summary->tsv; # print results in tabular form __DATA__ U1 ID1 100 280 U1 ID1 137 250 U2 ID2 150 375 U1 ID2 100 100 U3 ID1 100 600 U9 ID3 137 200

Update: Added suppression of other columns


In reply to Re: Perl Sum & Count column based data by hdb
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.