in reply to Re^4: how to identify a null hash value
in thread how to identify a null hash value

Ok, so you want to see if the hash in that value is empty... I think I get you now ;) Try this:

if (%{$hh{'QUESTION_TEXT'}}){ # do stuff... } else { # it's empty, so do different stuff... }

It dereferences the inner hashref, and checks if it isn't null (empty).

-stevieb

Replies are listed 'Best First'.
Re^6: how to identify a null hash value
by aaron_baugher (Curate) on Jun 04, 2015 at 23:01 UTC

    In addition to stevieb's correct answer, I'll add: there's no such thing as "NULL" in Perl, so that may cause some confusion. Values in Perl may be defined or undefined, as well as true or false. But references (pointers to other things) are always defined and true, even if the thing they point to is empty, as in the case of a hash with no keys and values.

    #!/usr/bin/env perl use 5.010; use warnings; use strict; my %hash = (); # create empty hash my $ref = \%hash; # create reference pointing to empty hash if($ref){ say 'Reference is true'; # <- always true } else { say 'Reference is false'; } if (%$ref){ # derefence hash and check the number of elements in it say 'Hash has elements'; } else { say 'Hash is empty'; # <-- hash has no elements } $hash{a} = 1; # add a key and value to hash if (%$ref){ # derefence hash and check the number of elements in it say 'Hash has elements'; # <-- now it has one key and one value } else { say 'Hash is empty'; }

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      As I said, Stevie's response did not work under "use strict". What finally worked is checking for the literal value of the string "HASH", which is what was printing out for me.

      if (!($hh{'QUESTION_TEXT'} =~ /HASH\(/)) {

      Now, I thought it printed out this value when you tried to print a hash reference that wasn't defined. I don't understand this, since the Dumper call shows the structure to be like the below:

      {
      'QUESTION_TEXT' => {},
      'VARNAME' => 'OTHER STUFF',
      },

      So why does my code need to check for literally "HASH(" in the string, when the structure clearly has nothing in it?

        When a reference to a hash is evaluated in scalar context, as in your if statement, Perl stringifies it into something like HASH(0x80182c2b8), where the hexadecimal number is the memory location where the actual hash is stored. So by checking for /HASH\(/, you can see that it's a hash reference (or a string that looks like one), but that doesn't tell you whether the hash it points to has any keys and values. Your if(! test will only return true if the value for the 'QUESTION_TEXT' key isn't a hash reference at all. Try this to prove it:

        $h = {}; # create reference to an empty hash print $h; # prints HASH(0xSOMETHING)

        It sounds like what's happening in your case is that $hh{QUESTION_TEXT} sometimes holds a hash reference, and sometimes holds a string. You can check for that first with the ref function. That will tell you whether the value is a hash reference, and if it is, then you can dereference it as stevieb and I both showed, to see if it's empty.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.

Re^6: how to identify a null hash value
by smartyollie (Novice) on Jun 04, 2015 at 22:46 UTC
    This did not work. This complains under "use strict" rules:

    Can't use string ("Some actual text here"...) as a HASH ref while "strict refs" in use ...

    When I ran without "use strict", it ALWAYS executed the else clause.