You say you are having difficulty, but you don't say what the difficulty is very clearly, making it difficult to help.

Perhaps the following will give you some ideas. Otherwise, you might be a bit more specific about what the difficulty is.

#!C:/strawberry/perl/bin/perl.exe # # See http://rick.measham.id.au/pdf-api2/ # use strict; use warnings; use Data::Dumper::Concise; my @records; while(my $record = <DATA>) { push(@records, [ split(/\s+/, $record) ]); } report({ group_by => 0, sum_columns => [ 2, 3 ], records => \@records, }); report({ group_by => 1, sum_columns => [ 2, 3 ], records => \@records, }); report({ group_by => 2, sum_columns => [ 3 ], records => \@records, }); exit(0); sub report { my ($args) = @_; # sum columns by group my %groups; foreach my $record (@{$args->{records}}) { my $group = $groups{$record->[$args->{group_by}]} //= {}; $group->{count}++; foreach my $sum_column (@{$args->{sum_columns}}) { $group->{sum}->{$sum_column} += $record->[$sum_column]; } } # produce output print "example output: group by column $args->{group_by}, sum colu +mns " . join(', ', @{$args->{sum_columns}}) . "\n"; foreach my $group (sort keys %groups) { print join(' ', $group, map { ( $groups{$group}->{count}, $groups{$group}->{sum}->{$_} ) } @{$args->{sum_columns}} ) . "\n";; } print "\n"; } __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

Which produces:

example output: group by column 0, sum columns 2, 3 U1 3 337 3 630 U2 1 150 1 375 U3 1 100 1 600 U9 1 137 1 200 example output: group by column 1, sum columns 2, 3 ID1 3 337 3 1130 ID2 2 250 2 475 ID3 1 137 1 200 example output: group by column 2, sum columns 3 100 3 980 137 2 450 150 1 375

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