I'm afraid your last solution, as concise as it might be, simply does not work. \%h always refers to the same hash

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

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
Resulting in:
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' } };
case 2: the %h is declared outside the block and hence points to the same thing everytime:
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
Resulting in this:
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'} };
And lastly (have to remove use strict; .. gasp). Case 3: No my (thus not lexically scoped, bad voodoo magic.)
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
Resulting in this:
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'} };
The differences are subtle, but jdporter's code works.

-enlil


In reply to Re: Re: Re: Hash of hashes syntax by Enlil
in thread Hash of hashes syntax by jens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.