Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: Perl Array of Hashes

by abhay180 (Acolyte)
on Jul 27, 2009 at 05:55 UTC ( #783443=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://783443]
[ambrus]: MLX: if it's a work email, then it's probably not Uncle Sam that matters, but what the account managing server at work thinks your name is. Those can differ. For example, we've had two co-workers with identical real name at one point,
[ambrus]: so one got a stupid suffix in the email account (people have email address based on their real name here usually).

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2017-01-19 14:05 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.