http://www.perlmonks.org?node_id=783443


in reply to Re: Perl Array of Hashes
in thread Perl Array of Hashes

Hello, Thnaks a ton for all ur suggestions. I wanted to do little more here...
my $k=0,$str=""; for(my $i=0; $i<4;$i+=1) { @entry=undef; for my $j (0..7) { $h{"BUCK_$j"} = $bucket[$k+$j]; push @entry,\%h; } $hash_table[$i] = { ENTRY => [@entry]}; $k+=8; } #end of for i for(my $i=0; $i<4;$i+=1) { print "ENTRY-0-BUCK-0::", $hash_table[$i]->{ENTRY}->{BUCK_0}, "\n"; }
The print of "ENTRY-0-BUCK-0" doesnt work....what is that i am doing wrong here?

Replies are listed 'Best First'.
Re^3: Perl Array of Hashes
by mzedeler (Pilgrim) on Jul 27, 2009 at 07:19 UTC

    Did you use warnings and use strict when you ran the code?

    But that aside, you are doing some strange things:

    my $k=0,$str=""; for(my $i=0; $i<4;$i+=1) { @entry=undef; for my $j (0..7) { $h{"BUCK_$j"} = $bucket[$k+$j]; push @entry,\%h; }

    The loop above will add 8 identical references to %h to @entry.

    $hash_table[$i] = { ENTRY => [@entry]}; $k+=8; } #end of for i for(my $i=0; $i<4;$i+=1) { print "ENTRY-0-BUCK-0::", $hash_table[$i]->{ENTRY}->{BUCK_0}, "\n";

    You can't address $hash_table like that since it is an array of hash references containing just the key ENTRY (which is a warning sign) that has the value array reference containing hash references.

    }

    Did you try following the advice and tried Data::Dumper on, say, (updated) @hash_table?

      Yes, By using Dumper,I was able to correctly construct the data-structure i needed. Thanks a lot for all ur help.
      Yes, with Dumper, i am able to generate data-structure of my choice. Thanks a ton.