cmic has asked for the wisdom of the Perl Monks concerning the following question:
And the code prints this (unexpected) :my %x= ('miller'=> {'lastname' => 'michael','tel'=>'222'}, 'duran' => {'lastname'=> 'peggy', 'tel' => '333'}, ); print "--Original hash \n"; for my $nom (keys %x) { print "nom: ", $nom, " "; for my $val (keys %{$x{$nom}} ) { print $val, ": ", $x{$nom}{$val}, " "; } print"\n"; } my $y= {%x}; #anonymous hash ref containg data freom %x # #modifying $x shouldn't affects $y ?? # $x{'duran'}{'tel'}='888'; print "--Deep copy of Original hash \n"; #notice how to dereference $y !! for my $nom (keys (%$y)) { print "nom: ", $nom, " "; for my $val (keys %{@$y{$nom}}) { print $val, ": ", $y->{$nom}{$val}, " "; } print "\n"; } </h3>
--Original hash nom: miller lastname: michael tel: 222 nom: duran tel: 333 lastname: peggy --Deep copy of Original hash nom: duran tel: 888 lastname: peggy nom: miller lastname: michael tel: 222
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: deep copy of hash of hash
by haukex (Archbishop) on May 24, 2019 at 16:57 UTC | |
by cmic (Acolyte) on May 27, 2019 at 09:25 UTC | |
by hippo (Archbishop) on May 27, 2019 at 10:07 UTC | |
by johngg (Canon) on May 27, 2019 at 09:34 UTC |