thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

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?

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to check if hash has empty hash reference?
by hippo (Archbishop) on Feb 15, 2017 at 15:08 UTC

    Does this help?

    #!/usr/bin/env perl use strict; use warnings; my %hash = ( key1 => 'value', key2 => undef, key3 => {}, key4 => { foo => 99 }, ); foreach my $key (qw(key1 key2 key3 key4)) { my $reftype = ref ($hash{$key}); if ($reftype && $reftype eq 'HASH') { my $entries = scalar keys %{$hash{$key}}; print "\$hash{$key} is a hashref which is " . ($entries ? 'not ' : '') . "empty.\n"; } else { print "\$hash{$key} is not a hashref\n"; } }

    Count the items to see if a hashref is empty or not.

      Hello hippo

      Thanks your solution pointed me to right direction, to finally resolved my issue on my code.

      Thanks again for your time and effort assisting me.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to check if hash has empty hash reference?
by tybalt89 (Monsignor) on Feb 15, 2017 at 15:12 UTC
    delete @hash{ grep ref($hash{$_}) eq 'HASH' && %{$hash{$_}} == 0, keys + %hash };

      Hello tybalt89

      Thanks for the one liner. I also some times like to write everything in one line. It looks simple neat and clean.

      Thanks again for your time and effort assisting me.

      Seeking for Perl wisdom...on the process of learning...not there...yet!