我有一个类似如下的输入:
atom 1 23
atom 1 13
atom 1 22
atom 1 24
atom 2 99
atom 2 98
atom 2 21
atom 3 15
atom 3 20
atom 4 19
atom 5 11我想对第三列求和,但它应该报告,如果它在第二列中读取1,它应该在第三列中给出所有1的值的总和。类似地,在第二列中,如果它是2,它应该在第三列中给出2的总和。类似地,在第二列中,如果它是三,那么它应该在第三列中给出总计三。同样的,直到文件结束。请帮我解决这个问题..
发布于 2013-12-02 14:28:33
也许下面的内容会对你有所帮助:
use strict;
use warnings;
my %hash;
while (<>) {
my @elems = split;
$hash{ $elems[1] } += $elems[2];
}
print "atom $_ $hash{$_}\n" for sort { $a <=> $b } keys %hash;用法:perl script.pl inFile [>outFile]
最后一个可选参数将输出定向到一个文件。
数据集上的输出:
atom 1 82
atom 2 218
atom 3 35
atom 4 19
atom 5 11https://stackoverflow.com/questions/20321904
复制相似问题