Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Hash reference Problem

by gamache (Friar)
on Dec 27, 2007 at 15:01 UTC ( [id://659208] : note . print w/replies, xml ) Need Help??

in reply to Hash reference Problem

It looks like in the first case, you end up storing a reference to a piece of an existing hash in $ref_1->{2}{testX}{1}, and in the second case you get the actual bits of hash rather than a reference to another place in which they exist.

Sounds like you want use Storable qw(dclone), which performs a "deep clone" of nested data structures:

use Storable qw(dclone); foreach my $b1 (keys %{$ref_1->{'1'}}) { if(exists $ref_1->{'2'}->{$b1}) { $ref_1->{'2'}->{$b1}->{'1'} = dclone $ref_1->{'1'}->{$b1}; } } print Dumper $ref_1;

Side note: In Perl, pointer syntax (->) is optional after the first one; that is, $ref_1->{1}->{test1}->{1} is equivalent to $ref_1->{1}{test1}{1}.