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

I have read an xml document using XML::Simple in a perl hash.I have accessed multiple sub hashes in my program.But at one line when I try to use a sub hash,it says "not a hash reference".So,what I would like is that if that sub hash exists,it performs one function and if that particular sub hash does not exist,it skips that part.Please suggest.

  • Comment on Non existent Hash and error"not a hash refernece at perl line"

Replies are listed 'Best First'.
Re: Non existent Hash and error"not a hash refernece at perl line"
by roboticus (Chancellor) on Aug 17, 2015 at 11:37 UTC

    If you're getting a message about it not being a hash reference, then it means you have something in the slot, but it's not a hash reference. You can check whether a hash slot exists with the perldoc -f exists. Here's a simple bit showing how to check several things out about a hash:

    $ cat foo.pl use strict; use warnings; my %hash = ( foo => undef, bar => { a=>7 }, baz => 'quandary', ); for my $k ('joe', 'foo', 'bar', 'baz') { if (! exists $hash{$k}) { print "Key $k does not exist in hash\n"; next; } if (! defined $hash{$k}) { print "Key $k exists, but value is not defined\n"; next; } my $r = ref($hash{$k}); $r = "SCALAR" if $r eq ''; print "Key $k exists, refers to ", $r, "\n"; print "looking up \$hash{$k}{a}: $hash{$k}{a}\n"; } Roboticus@Waubli ~ $ perl foo.pl Key joe does not exist in hash Key foo exists, but value is not defined Key bar exists, refers to HASH looking up $hash{bar}{a}: 7 Key baz exists, refers to SCALAR Can't use string ("quandary") as a HASH ref while "strict refs" in use + at foo.pl line 22.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: I want to check if a particular hash exists or not and not just the key.
by stevieb (Canon) on Aug 17, 2015 at 13:16 UTC

    Check for exists:

    if (! exists $hash{$key}){ ... next; }

    In the above, it's possible to just say if (! $hash{$key}), but if the hash key's value has something like 0 or anything else false (or undef), the if will be false.

    Check if hash key is another hash:

    if (! ref $hash{$key} eq 'HASH'){ ... next; }

    Now, with that said, you're probably best off showing us a piece of your structure and the code that accesses it. Many times, warnings like this are thrown due to the code accessing the structure is doing something we don't expect, and other times it may be because we are misunderstanding what the structure really is in the first place.

    -stevieb

    Re-parented this (my own) node from considered duplicate I want to check if a particular hash exists or not and not just the key.

Re: Non existent Hash and error"not a hash refernece at perl line"
by vinoth.ree (Monsignor) on Aug 17, 2015 at 09:24 UTC

    Post the hash you are using. so we can help you better.

    From the error message you posted, it seems you are trying to access a data structure as hash reference, but it doesn't hold a reference to a hash.

    It might be useful to dump your data structure to figure out what it actually is.

    use Data::Dumper; print(Dumper($datastructure));

    Post us the output of the Dumper() here and tell hash which part you are trying to access.


    All is well. I learn by answering your questions...
Re: Non existent Hash and error"not a hash refernece at perl line"
by anonymized user 468275 (Curate) on Aug 17, 2015 at 15:20 UTC
    Because XML will produce a nested structure of arrays and hashes at arbitrary depth, you'll anyway need to test what is found recursively. e.g.:
    # load XML into $tree here, then ... traverse ($tree); sub traverse { my $tree = shift; if (ref($tree) eq 'ARRAY') { traverse($_) for (@$tree); } elsif (ref($tree) eq 'HASH' ) { while (my ($k, $v) = each %$tree) { # process key $k traverse($v); } } elsif (!ref($tree)) { # process $tree as a scalar here } else { die "unexpected type of reference: " . ref($tree); } }

    One world, one people

A reply falls below the community's threshold of quality. You may see it by logging in.