Perl数组总和相似的元素

时间:2013-09-08 14:14:55

标签: arrays perl hash key duplicates

我有一个由字符串和数字组成的数组。我想总结那些具有相似字符串值的数字。请帮忙!!

INFILE:

aggr3 350.01000213623
aggr3 1228.79999923706
aggr5 250
aggr3 1536
aggr3 690.01000213623
aggr3 1587.20000076294
aggr9 550.01000213623
aggr3 1228
aggr5 905
aggr5 100

必需的输出

aggr3 5391
aggr5 1255
aggr9 550

2 个答案:

答案 0 :(得分:3)

或许这样吗?

use strict;
use warnings;

my @data = <DATA>;
my %data;
$data{$_->[0]} += $_->[1] for map [ split ], @data;

print "$_ $data{$_}\n" for sort keys %data;

__DATA__
aggr3 350.01000213623
aggr3 1228.79999923706
aggr5 250
aggr3 1536
aggr3 690.01000213623
aggr3 1587.20000076294
aggr9 550.01000213623
aggr3 1228
aggr5 905
aggr5 100

<强>输出

aggr3 6620.02000427246
aggr5 1255
aggr9 550.01000213623

答案 1 :(得分:2)

通常,如果要在Perl中对某些数据进行分组,则use hashes。这些哈希的键对应于分组标准,值用作累加器(它可以是简单的数字,在这种情况下,或者等待稍后处理的数字数组)。

这是一种方法:

use warnings;
use strict;

# this hash will hold all the cumulatives
my %sums;

# here we scan the source, line by line
# each line is split to key and value
while (<DATA>) {
  chomp;
  my ($label, $value) = split;

  # this line uses the auto-vivification Perl feature:
  # if there's no corresponding item in %sums hash, it'll be created (with 0 value)
  $sums{$label} += $value;
}

# here we process the resulting hash:
for my $key (sort keys %sums) {
  print $key, ' ', $sums{$key}, "\n";
}
__DATA__
aggr3 350.01000213623
aggr3 1228.79999923706
aggr5 250
aggr3 1536
aggr3 690.01000213623
aggr3 1587.20000076294
aggr9 550.01000213623
aggr3 1228
aggr5 905
aggr5 100

Codepad demo