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!

In reply to How to check if hash has empty hash reference? [RESOLVED] by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.