简体   繁体   English

Perl计算一列与另一列的总和

[英]Perl count the sum of one column aggregating by another

I have a dataset will a lot of columns. 我有一个数据集将有很多列。 What I need to do is to sum a aggregate a certain column in terms of another. 我需要做的是将某个列的总和与另一个列相加。 As an example, 举个例子,

ID       Volume
A          20
D          60
B          10
A          50
K          30 
B          100
D          80 

So I want an aggregated sum of all the different IDs (A, B, C...) in terms of volumes and sorted by that sum 所以我想要所有不同ID(A,B,C ...)的合计总和,并按总和排序

The result would be like 结果就像

D           140
B           110
A           70
K           30

how would I accomplish this in perl? 我将如何在perl中完成此任务?

  #!/usr/bin/perl

  use strict;
  use warnings;

  my %ids_and_sums;

  while (<>) {
     # The regex will only consider one single uppercase letter as
     # an ID; in case your IDs may look different, you could prepend
     # your 'ID  Volume' line with a character which will never be part
     # of an ID, and modify below regex to meet your needs
     my ($id, $volume) = m/^([A-Z])\s+(\d+)/;

     if ($id and $volume) {
        $ids_and_sums{$id} += $volume;
     }
  }

  foreach my $key (sort {$ids_and_sums{$b} <=> $ids_and_sums{$a}} keys %ids_and_sums) {
     print "$key: $ids_and_sums{$key}\n";
  }

This prints: 打印:

D: 140
B: 110
A: 70
K: 30

EDIT: I have modified the code so that the sorting will be in descending order of the sums. 编辑:我已经修改了代码,以便排序将以总和的降序排列。

You can do it as: 您可以按照以下方式进行操作:

perl -lnae '$H{$F[0]} += $F[1];END { print $_." ".$H{$_} for(keys %H) }'

passing it all but the first line of your input file as standard input. 将输入文件的第一行以外的所有内容作为标准输入传递。

Ideone Link Ideone链接

You can make Perl discard the heading line as: 您可以使Perl放弃标题行为:

perl -lnae 'BEGIN{$i=1;}if($i){$i=0;next;}$H{$F[0]} += $F[1];END { print $_." ".$H{$_ } for(keys %H)  }' file

Ideone Link Ideone链接

$, = ' ';   # set output field separator
$\ = "\n";    # set output record separator

while (<>) {
    ($Fld1,$Fld2) = split(' ', $_, -1);
    $map{$Fld1} += $Fld2;
}

foreach $i (keys %map) {
    print $i, $map{$i};
}

something like this 像这样的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM