0
關於如何check if a hash is empty有很多參考。我在以下腳本中嘗試了它們:爲什麼檢查散列是否爲空失敗?
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
# Signals come...
my %items = (
item_1 => {signal_1 => 'up',
signal_2 => 'down',
signal_3 => 'up',
},
item_2 => {signal_1 => 'down',
},
item_3 => {signal_1 => 'up',
signal_3 => 'down',
signal_4 => 'down',
signal_5 => 'down',
},
);
# ... and signals go:
delete $items{'item_2'}->{'signal_1'};
# and sometimes all signals are gone from an item:
print Dumper(\%items);
# in that case we would like the signal count to show 0 (zero).
my %signals;
foreach my $item (sort keys %items){
#$signals{$item} = 0;
foreach my $signal (sort keys %{$items{$item}}){
if (not %{$items{$item}}) {print "HERE\n"; $signals{$item} = 0}
elsif($items{$item}->{$signal} eq 'up') {$signals{$item}++}
elsif($items{$item}->{$signal} eq 'down'){$signals{$item}--}
}
}
# unfortunately the item disappears completely!
print Dumper(\%signals);
看起來迭代器完全跳過了空的散列。我產生正確結果的唯一方法是將每個計數初始化爲零(註釋掉),並讓其通過非空散列進行遞增/遞減。
爲什麼...?
@Toto我認爲這個問題是爲什麼沒有'item_2 => 0'。 – simbabque
@simbabque:好的,我誤解了:( – Toto
)如果有X個鍵要迭代,那麼這個循環有X個迭代,是的,即使X爲零也是如此。你的「解決方法」實際上是正確的處理這種情況。 – ikegami