Perl, check if pair exists in hash of hashes -


in perl, have hash of hashes created loop similar following

    %hoh     $i (1..10) {       $hoh{$a}{$b} = $i;      } 

$a , $b variables have value when hoh gets filled in. after creating hoh, how can check if particular pair ($c, $d) exists in hoh? following not work

if (defined $hoh{$c}{$d}) {...} 

because if $c not exist in hoh already, created key without value.

use data::dumper;  %hoh;  $hoh{a}{b} = 1;  if(exists $hoh{c} && exists $hoh{c}{d}) {    print "exists\n"; }  print dumper(\%hoh);  if(exists $hoh{c}{d}) {    print "exists\n"; }  print dumper(\%hoh); 

output:

$var1 = {           'a' => {                    'b' => 1                  }         }; $var1 = {           'a' => {                    'b' => 1                  },           'c' => {}         }; 

autovivification causing keys created. "exists" in second example shows first example checks both keys individually.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -