tsmoyers has asked for the wisdom of the Perl Monks concerning the following question:
If I use exists() to test for a subkey, it creates a new instance. To demonstrate, use the above hash with this code:my %List; $List{"a"}{"1"} = "yes"; $List{"a"}{"2"} = "no"; $List{"b"}{"1"} = "possibly"; $List{"b"}{"2"} = "never";
This will give the output:print "Before:\n"; foreach my $priKey (sort keys %List) { print "$priKey\n"; } if (exists $List{"c"}{"1"} ) { print "found a bad one\n"; } print "After:\n"; foreach my $priKey (sort keys %List) { print "$priKey\n"; }
Is this correct behavior that I need to code around or is it a bug? It seems to me that exists should never create a key, being a test-only operation, but I wanted to see if the Monks agree. The perl version used for the above is 5.6.1 on Solaris, but it appears to be acting the same way on other versions.Before: a b After: a b c
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unexpected exists() behavior
by davido (Cardinal) on Jun 22, 2006 at 16:42 UTC | |
by ikegami (Patriarch) on Jun 22, 2006 at 16:57 UTC | |
|
Re: Unexpected exists() behavior
by Fletch (Bishop) on Jun 22, 2006 at 16:29 UTC | |
|
Re: Unexpected exists() behavior
by jeffa (Bishop) on Jun 22, 2006 at 16:18 UTC | |
by tsmoyers (Pilgrim) on Jun 22, 2006 at 16:27 UTC | |
by jeffa (Bishop) on Jun 22, 2006 at 16:51 UTC | |
by Fletch (Bishop) on Jun 22, 2006 at 17:36 UTC | |
|
Re: Unexpected exists() behavior
by Moron (Curate) on Jun 23, 2006 at 15:24 UTC |