After you delete the two entries inside %{$books{-1}}
, the hash reference $books{-1}
still exists even though it's empty.
use strict;
use warnings;
my %book = (
+1 => {
27 => 100,
24 => 1000,
},
-1 => {
30 => 200,
31 => 500,
}
);
delete $book{-1}{30};
delete $book{-1}{31};
use Data::Dump;
dd \%book;
Outputs:
{ -1 => {}, 1 => { 24 => 1000, 27 => 100 } }
If you'd like to trim that entry, you'll have to code explicit logic for that
# Trim Hash
my @empty_keys = grep {!%{$book{$_}}} keys %book;
delete $book{$_} for @empty_keys;
Update
As ysth suggested, one alternative solution would be to test if any of the hash keys were empty instead:
if (! grep {!$book{$_} || !%{$book{$_}}} qw(1 -1)) {
print "A\n";
} else {
print "B\n";
}