megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Dear learned monks,

I have created a hoh as below:

my %hoh = ( a => { a1 => { a11 => 1, a12 => 1, }, b1 => { b11 => 1, b12 => 1, }, c1 => 1, }, b => { --- some elements like above--- }, );

Now i have a get accessor for this, which returns

return $hoh{'a'};

What i understand is that it should return the whole hash which is value for key a. But i am getting only the first element 'a1' i.e.

a1 => { a11 => 1, a12 => 1, }

Any ideas why this is happening?

Thanks,
Gaurav
"Wisdom begins in wonder" - Socrates, philosopher

Replies are listed 'Best First'.
Re: hash question
by GrandFather (Saint) on Dec 12, 2008 at 00:39 UTC

    Works for me:

    use strict; use warnings; use Data::Dump::Streamer; my %hoh = ( a => {a1 => {a11 => 1, a12 => 1,}, b1 => {b11 => 1, b12 => 1,}, c1 + => 1,}, b => {a2 => {a22 => 2, a22 => 2,}, b2 => {b22 => 2, b22 => 2,}, c2 + => 2,}, ); Dump (get ()); sub get { return $hoh{'a'}; }

    Prints:

    $HASH1 = { a1 => { a11 => 1, a12 => 1 }, b1 => { b11 => 1, b12 => 1 }, c1 => 1 };

    How about you show us your equivelent code that doesn't work?


    Perl's payment curve coincides with its learning curve.

      Thanks for the reply. Outside my application it worked for me too. So i realised problem is somewhere in the application. And then i found one of the subroutines was actually deleting these keys :)

      Gaurav
      "Wisdom begins in wonder" - Socrates, philosopher

        Putting together a runnable test case that demonstrates the problem often has that effect. It's very like talking to the Teddy bear.


        Perl's payment curve coincides with its learning curve.