Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: usage of if on nested hash creating empty slot for non existing value

by cdarke (Prior)
on Jan 29, 2011 at 15:30 UTC ( [id://885025]=note: print w/replies, xml ) Need Help??


in reply to usage of if on nested hash creating empty slot for non existing value

I don't get the symptoms you describe, although I had to alter your code to get it to compile:
use strict; use warnings; use Data::Dumper; my $hash; $hash = {A => { LEVEL1 => { LEVEL1_2 => { LEVEL1_2_3 => { VAL => 'somevalue' } } } } }; for (my $i = 0; $i< 3; $i++) { if (defined $hash->{A}->{LEVEL2}->{LEVEL2_2}) { # do something print "if branch\n"; } elsif (defined $hash->{A}->{LEVEL1}->{LEVEL1_2}) { #do something print "elsif branch\n" } else { print "else branch\n" } } print Dumper($hash);
gives:
elsif branch elsif branch elsif branch $VAR1 = { 'A' => { 'LEVEL1' => { 'LEVEL1_2' => { 'LEVEL1_2_3' => { 'VA +L' => 'somevalue' } } }, 'LEVEL2' => {} } };
The key 'LEVEL2' is added by autovivification as described by Anonymous Monk, but not 'LEVEL2_2' as you imply, so it does not go through the 'if'. Could you post some code that compiles and shows the problem you have?
I'm not sure if this is relevant, but if you want to check for the existence of a key use exists, defined checks for the existence of a value.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://885025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2025-11-15 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.