【发布时间】:2020-10-26 23:29:53
【问题描述】:
我正在像这样初始化一个哈希表:-
my %AllCountStats = ();
foreach my $Log (@LogList) {
foreach my $Func (keys %AllFuncNames) {
push @{$AllCountStats{$Log}}, {Func=>$Func,Count=>0};
}
}
print Dumper (\%AllCountStats);
Dumper 输出如下所示:-
$VAR1 = {
'log.1' => [
{
'Count' => 0,
'Func' => 'Function A'
},
{
'Func' => 'Function B',
'Count' => 0
},
}
'log.2' => [
{
'Count' => 0,
'Func' => 'Function A'
},
{
'Count' => 0,
'Func' => 'Function X'
},
};
现在我需要遍历散列数组的散列,并通过手术更新每个 Func 的 Count 值。使用上面的示例,我应该发出什么命令来将 log.1 的 Func=Function A 值更新为新的值(即不是 0)?这是我尝试在何处/如何进行更新的示例...
foreach $Log (@LogList) {
foreach (sort {$a->{SCmdLineNum} <=> $b->{SCmdLineNum}} @{$SweepStats{$Log}}) {
$SCmd = $_->{SCmd};
my $inner = $AllCountStats{$Log}{$SCmd}{Count};
$inner->{$_}++ for keys %$inner;
}
}
但它不起作用。当 $inner 有效地变为 $AllCountStats{log.1}{Function B}{Count} 时,我怎样才能干净地更新它的 Count 值?
【问题讨论】:
-
%SweepStats来自哪里?请尝试编辑代码,以便我们运行它并重现您的问题。 “不工作”是什么意思? -
嗨,@choroba,您实际上不需要关心 %SweepStats——主要问题是如何通过手术更新 %AllCountStats 中的 Count 字段。当我尝试发出 $inner->{$_}++ 时,它报告上一行是非法的哈希引用,所以我认为它的格式不正确。
-
Re "你真的不需要关心 %SweepStats",好吧,没错,但我们确实需要知道你想知道哪个
Count@{$SweepStats{$Log}}的给定元素的增量。 -
提示:
sort没用。循环中的任何内容都不关心访问元素的顺序。