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

When I define our_hash like:
$our_hash = { 'this' => 'that', 'foo' => ['bar', 'and', 'something', 'else'] };
And then do this:
$$our_hash{'somekey'} = { 'foo' => $$our_hash{'foo'} };
our_hash turns into:
$our_hash = { 'this' => 'that', 'foo' => [], 'somekey' => { 'foo' => ['bar', 'and', 'something', 'else'] } };
What I'm trying to do is get $our_hash->{'somekey'}->{'foo'} to have the same value as $our_hash->{'foo'}

I have no idea what I did wrong!

Thanks in advance!

Replies are listed 'Best First'.
Re: Hash/array madness
by Fletch (Bishop) on Jul 25, 2002 at 02:08 UTC
    $our_hash->{somekey}->{foo} = $our_hash->{foo}

    See perldoc perlreftut, perldoc perlref

Re: Hash/array madness
by sauoq (Abbot) on Jul 25, 2002 at 02:25 UTC
    I have no idea what I did wrong!

    I don't either. The code you posted is correct (although the style is a little rough.) I suspect you are doing something else you aren't telling us about if the hash you reference really ends up being what you say. I suggest using Data::Dumper to take a look at what you really have.

    Update: I double checked myself. I cut and pasted the code you provided and used Data::Dumper as I suggested. The output was exactly what I expected:

    $VAR1 = { 'foo' => [ 'bar', 'and', 'something', 'else' ], 'this' => 'that', 'somekey' => { 'foo' => $VAR1->{'foo'} } };

    As you can see, $VAR1->{'foo'} is exactly equal to $VAR1->{'somekey'}{'foo'} as they both refer to the same array. You should, BTW, keep that in mind if you change an element of that array. Do you want $our_hash->{'somekey'}{'foo'} to refer to the same array referred to by $our_hash->{'foo'} or do you want it to refer to an array which is a copy of the one referred to by $our_hash->{'foo'}?

    -sauoq
    "My two cents aren't worth a dime.";