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

I have a large multilevel hash generated from XML via XML::Simple module. I have several hash elements that are null, and I'd like to print nothing in those cases, but my program still continues to print the HASH item.

from dumping the structure using Data::Dumper:

'QUESTION_TEXT' => {},

I've tried various ways to identify that null hash element but no luck.

my $value = $hh{'QUESTION_TEXT'}; if (CHECKSOMETHING) { print "$value"; } else { print "NULL"; }

It always prints something like HASH(0x31f4348), i.e. it appears the else clause is never executing. for CHECKSOMETHING I've tried the following, but nothing ends up executing the ELSE case:

if ($value ne '') if (defined($value) if (exists($value) if (!($value =~ /[a-zA-Z0-9]/)) {

Replies are listed 'Best First'.
Re: how to identify a null hash value
by kcott (Archbishop) on Jun 04, 2015 at 22:47 UTC

    G'day smartyollie,

    There's a semantic issue here. Hashes consist of zero or more pairs of keys and values: talking about a "null hash value" probably isn't what you meant.

    I suspect, from the code you've posted, you meant a hash without any key/value pairs. If so, keys will probably do what you want. Here's an example:

    #!/usr/bin/env perl -l use strict; use warnings; my %multi = (empty => {}, data => { a => 1 }); for my $key (keys %multi) { if (keys %{$multi{$key}}) { print "Data found in $key"; } else { print "Data not found in $key"; } }

    Output:

    $ pm_1129100_complex_hash_check.pl Data found in data Data not found in empty

    If I haven't guessed correctly, please read "Checking Variable for Not Null". This was posted last week and contains a discussion about NULL not being a Perl concept, per se. Having done that, please reword your question, replacing "null" with undefined, zero-length, FALSE or whatever you feel is most appropriate.

    -- Ken

      What I have is a key/value pair, where the value is a zero-length value. I was referring to that as "null".

        Then you'll want the length() function which can be found in "Perl functions A-Z".

        -- Ken

Re: how to identify a null hash value
by stevieb (Canon) on Jun 04, 2015 at 20:29 UTC

    So it looks like the hash %hh has a key of 'QUESTION_TEST', who's value is itself another hash, so $value will always be a reference to that hash.

    I think you need to post more code above this section, particularly where %hh hash is being defined and set up. You also need to explain what CHECKSOMETHING actually is.

    -stevieb

      I've tried various things for CHECKSOMETHING, see the four IF statements I listed at the end of my post.

        ahhh, I see. Again though, it does appear from what you've posted that $value DOES contain data, and if that's the case, all of your tests will be true.

        One way to see if I'm correct in thinking you've got a defined value (hashref) is this:

        print "it's a hashref\n" if ref($hh{'QUESTION_TEXT'}) eq 'HASH';

        -stevieb

Re: how to identify a null hash value
by Hosen1989 (Scribe) on Jun 05, 2015 at 08:41 UTC

    you can try the next code as its worked for me:

    my %hh = ('QUESTION_TEXT' => 0); # my %hh = ('QUESTION_TEXT' => {}); # my %hh = ('QUESTION_TEXT' => {'F'=> 3}); my $value = $hh{'QUESTION_TEXT'}; if(ref($value) eq 'HASH'){ $HashSize = @TemArr = keys $hh{'QUESTION_TEXT'}; # to get hash +size to tesitng }else{ $HashSize = 1; # not hash (may be ARRAY or SCALAR or ... etc) } # print "--[$HashSize]\n"; if($HashSize){ print "[$value]\n"; }else{ print "[NULL]\n"; }
      Thank you, this works although it gives me a perl warning
      keys on reference is experimental at <program>.pl line NNN.
      How to suppress?

        keys %{ $hh{'QUESTION_TEXT'} } or better yet, IMHO,  keys %$value (untested).


        Give a man a fish:  <%-(-(-(-<