我想计算散列中数组的值并将它们添加到散列中。我的代码如下所示:
while(my $line=<$fh>) {
$timestamp = $1 if $line=~ /^\s*<timestamp>(.*)</;
$timestamp =~ s/^(\d\d\d\d-\d\d-\d\d)T.*\s*$/$1/;
$errorCode= $1 if $line=~ /^\s*<errorCode>(.*)</;
$hash{$timestamp} = {} unless($hash{$timestamp});
$hash{$timestamp}{$errorCode} = [] unless($hash{$timestamp}{$errorCode});
push @{$hash{$timestamp}{$errorCode}}, $1 if $line =~ /<errorText>(.*)</;
}
输出
'2019-04-05' => { '5005' => [
'Dies ist kein aktives Konto',
'Dies ist kein aktives Konto'
],
'7112' => [
'Eingabefelder nicht richtig gefuellt.',
'Eingabefelder nicht richtig gefuellt.',
'Eingabefelder nicht richtig gefuellt.'
],
}
我想要的是这样的:
'2019-04-05' => { '5005' => { 'Dies ist kein aktives Konto' => 2 },
'7112' => { 'Eingabefelder nicht richtig gefuellt.' => 3 },
}
谁能帮我这个?提前致谢。
你可以做
while (my $line=<$fh>) {
$timestamp = $1 if $line=~ /^\s*<timestamp>(.*)</;
$timestamp =~ s/^(\d\d\d\d-\d\d-\d\d)T.*$/$1/;
$errorCode= $1 if $line=~ /^\s*<errorCode>(.*)</;
$hash{$timestamp}{$errorCode}{$1}++ if $line =~ /<errorText>(.*)</;
}
无需检查中间结构是否存在并手动创建它们。Autovivification 会解决这个问题。
只需添加另一层哈希键访问并增加值。你最终会得到一个值的计数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句