我有一个返回此哈希结构的 Perl 脚本:
$VAR1 = {
'Week' => [
'1238',
{
'OUT3FA_5' => 65,
'OUT3A_5' => 20,
'OUT3-Fix' => 45,
'IN1' => 85
},
'1226',
{
'OUT3FA_5' => 30,
'OUT3A_5' => 5,
'OUT3-Fix' => 25,
'IN1' => 40
}
]
};
我想做的是,计算IN1
每周的总数,每个例子在这种情况下它会返回:
$VAR1 = {
'Week' => [
'1238',
{
'OUT3FA_5' => 65,
'Total_IN1' => 85,
'OUT3A_5' => 20,
'OUT3-Fix' => 45,
'IN1' => 85
},
'1226',
{
'OUT3FA_5' => 30,
'Total_IN1' => 125,
'OUT3A_5' => 5,
'OUT3-Fix' => 25,
'IN1' => 40
}
]
};
以此类推,每周一次。
请问我该怎么做?任何帮助,将不胜感激。
这是我试图做的,但它不起作用:
my @sum_IN1 = qw(IN1); #kinda useless to use an array just for one value...
for my $num (keys %hash) {
my $found;
my $sum = 0;
for my $key (@sum_IN1) {
next unless exists $hash{$num}{$key};
$sum += $hash{$num}{$key};
$found = 1;
}
$hash{$num}{Total_IN1} = $sum if $found;
}