in reply to Re: Re: Hash of hashes syntax
in thread Hash of hashes syntax
Not true.
In this case, it would refer to a different hash. This is because the variable my %h is lexically scoped, and thus it is recreated each time through the loop, and so \%h will point to a different hash each time through the loop. To illustrate what I mean here is some code:
Case 1: %h lexically scoped inside the block each time through
Resulting in:use strict; use warnings; use Data::Dumper; my %hash_of_hashes; while (<DATA>) { my %h; (my $rn, @h{ qw(lastname firstname DOB funding URNo Photo_permission) }) = (split /,/)[0,1,2,7,18,15,14]; $hash_of_hashes{$rn} = \%h; } foreach (keys %hash_of_hashes) { print $hash_of_hashes{$_},$/; } print "DATA DUMPER SAYS:\n", Dumper \%hash_of_hashes; __DATA__ a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp aaa,abb,acc,add,aee,aff,agg,ahh,aii,ajj,akk,all,amm,ann,aoo,app
case 2: the %h is declared outside the block and hence points to the same thing everytime:HASH(0x1824898) HASH(0x224fb4) HASH(0x224ef4) DATA DUMPER SAYS: $VAR1 = { 'a' => { 'URNo' => 'p ', 'firstname' => 'c', 'funding' => undef, 'DOB' => 'h', 'lastname' => 'b', 'Photo_permission' => 'o' }, 'aaa' => { 'URNo' => 'app ', 'firstname' => 'acc', 'funding' => undef, 'DOB' => 'ahh', 'lastname' => 'abb', 'Photo_permission' => 'aoo' }, 'aa' => { 'URNo' => 'pp ', 'firstname' => 'cc', 'funding' => undef, 'DOB' => 'hh', 'lastname' => 'bb', 'Photo_permission' => 'oo' } };
Resulting in this:use strict; use warnings; use Data::Dumper; my %hash_of_hashes; my %h; while (<DATA>) { (my $rn, @h{ qw(lastname firstname DOB funding URNo Photo_permission) }) = (split /,/)[0,1,2,7,18,15,14]; $hash_of_hashes{$rn} = \%h; } foreach (keys %hash_of_hashes) { print $hash_of_hashes{$_},$/; } print "DATA DUMPER SAYS:\n", Dumper \%hash_of_hashes; __DATA__ a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp aaa,abb,acc,add,aee,aff,agg,ahh,aii,ajj,akk,all,amm,ann,aoo,app
And lastly (have to remove use strict; .. gasp). Case 3: No my (thus not lexically scoped, bad voodoo magic.)HASH(0x182485c) HASH(0x182485c) HASH(0x182485c) DATA DUMPER SAYS: $VAR1 = { 'a' => { 'URNo' => 'app ', 'firstname' => 'acc', 'funding' => undef, 'DOB' => 'ahh', 'lastname' => 'abb', 'Photo_permission' => 'aoo' }, 'aaa' => $VAR1->{'a'}, 'aa' => $VAR1->{'a'} };
Resulting in this:use warnings; use Data::Dumper; my %hash_of_hashes; while (<DATA>) { %h=(); (my $rn, @h{ qw(lastname firstname DOB funding URNo Photo_permission) }) = (split /,/)[0,1,2,7,18,15,14]; $hash_of_hashes{$rn} = \%h; } foreach (keys %hash_of_hashes) { print $hash_of_hashes{$_},$/; } print "DATA DUMPER SAYS:\n", Dumper \%hash_of_hashes; __DATA__ a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp aaa,abb,acc,add,aee,aff,agg,ahh,aii,ajj,akk,all,amm,ann,aoo,app
HASH(0x18248bc) HASH(0x18248bc) HASH(0x18248bc) DATA DUMPER SAYS: $VAR1 = { 'a' => { 'URNo' => 'app ', 'firstname' => 'acc', 'funding' => undef, 'DOB' => 'ahh', 'lastname' => 'abb', 'Photo_permission' => 'aoo' }, 'aaa' => $VAR1->{'a'}, 'aa' => $VAR1->{'a'} };
-enlil
|
|---|