in reply to Checking if hash value exists without implicitly defining it
One option is to short circuit the autovivification:
use strict; use warnings; my %Hash; $Hash{44.52}{param1}{key1} = [ 101.01, 'val1' ]; $Hash{95.01}{param2}{key2} = [ 101.02, 'val2' ]; my $neededVal = 80.0; if ( exists $Hash{$neededVal} and exists $Hash{$neededVal}{param1} and exists $Hash{$neededVal}{param1}{key1} ) { print "DEFINED"; } else { for ( keys %Hash ) { print "$_\n"; } }
Output:
95.01 44.52
Update I: Added the needed defined, in case of an earlier (for example) $Hash{$neededVal}{param1}{key1} = 0.
Update II: Replaced defined with exists. Thank you, choroba.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Checking if hash value exists without implicitly defining it
by Anonymous Monk on Feb 14, 2013 at 16:40 UTC | |
by Kenosis (Priest) on Feb 14, 2013 at 17:42 UTC | |
by choroba (Cardinal) on Feb 14, 2013 at 17:44 UTC | |
by Kenosis (Priest) on Feb 14, 2013 at 17:52 UTC | |
by choroba (Cardinal) on Feb 14, 2013 at 17:26 UTC | |
Re^2: Checking if hash value exists without implicitly defining it
by Anonymous Monk on Feb 15, 2013 at 10:04 UTC | |
by Kenosis (Priest) on Feb 15, 2013 at 15:47 UTC |
In Section
Seekers of Perl Wisdom