in reply to Perl Sum & Count column based data
use strict; use warnings; my $basecol=shift @ARGV or die "WHat base col?"; my %h; $basecol--; # zero based inside program. while (<DATA>){ chomp; my @f= split ; next unless @f > 1; my $k = $f[$basecol]; $h{$k}{COUNT}++; for my $col(0..$#f){ next if $col==$basecol; $f[$col]=0 unless $f[$col]=~/^\d+$/; $h{$k}{FIELD}[$col]+= $f[$col]; } } for (sort keys %h){ print "$_\t $h{$_}{COUNT}\t"; for my $f(@{ $h{$_}{FIELD} }){ defined $f or $f=''; print "$f\t"; } 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
When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Sum & Count column based data
by Anonymous Monk on Nov 21, 2013 at 18:22 UTC |