Hello Monks,
On my script I am deleting a value within a Hash of Hashes, but I can not delete the actual key that is associated with the value because it contains other values.
The problem appears when I am deleting the value and there are no defined values in the hash but just an empty key with not undef value but just empty.
I tried all possible ways that I could think on detecting this case so I could simply delete the key from the hash but I can not figure it out.
I found this relevant question (how to check if a hash reference is empty in perl) but it does not help on my problem.
Based on the check that I did the only solution that I could think is to create a negative check for both exist and defined but even that does evaluate as true.
Sample of code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( key1 => 'value', key2 => undef, key3 => {}, ); foreach my $key (qw(key1 key2 key3)) { print "\$hash{$key} exists: " . (exists $hash{$key} ? "yes" : "no" +) . "\n"; print "\$hash{$key} is defined: " . (defined $hash{$key} ? "yes" : + "no") . "\n"; print "\$hash{$key} is defined and not exist: " . ((!defined $hash +{$key} and !exists $hash{$key} ) ? "yes" : "no") . "\n"; } __END__ $hash{key1} exists: yes $hash{key1} is defined: yes $hash{key1} is defined and not exist: no $hash{key2} exists: yes $hash{key2} is defined: no $hash{key2} is defined and not exist: no $hash{key3} exists: yes $hash{key3} is defined: yes $hash{key3} is defined and not exist: no
Can someone help me understand where I am going so wrong?
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |