Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

deep copy of hash of hash

by cmic (Acolyte)
on May 24, 2019 at 16:51 UTC ( [id://11100475]=perlquestion: print w/replies, xml ) Need Help??

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

I thought deep copy of hash of hash would work, but when I modify the originla hash %x, the deep copy %y is modified too. What do i did wrong ?
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>
And the code prints this (unexpected) :
--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
-- cmic. Life helps. Perl Too.

Replies are listed 'Best First'.
Re: deep copy of hash of hash
by haukex (Archbishop) on May 24, 2019 at 16:57 UTC
    my $y = {%x};

    Although this does create a reference to a new anonymous hash and copies the key/value pairs (you can modify the keys of %$y without affecting the set of keys in %x), this is still not a deep copy - the values are still references to the same nested hashes ({'lastname' => 'michael','tel'=>'222'}) as in the original data structure.

    For true deep copies, see e.g. dclone from Storable or Clone.

    Minor improvements to wording.

      I get it. However, I loaded CPAN Clone module, but as I
      use Clone 'Clone'; #or even: use Clone::More qw( clone );
      Perl -c answers: "Clone" is not exported by the Clone module What should I do? (sorry to be a bit noob there...) TYA
      -- cmic. Life helps. Perl Too.

        Perl is case sensitive. Here is an SSCCE:

        use strict; use warnings; use Test::More tests => 2; use Clone 'clone'; my %x = ( miller => {lastname => 'michael', tel => '222'}, duran => {lastname => 'peggy', tel => '333'}, ); my %y = %{clone (\%x)}; is_deeply (\%x, \%y); $x{duran}{tel} = '444'; isnt ($x{duran}{tel}, $y{duran}{tel});

        The documentation says Use Clone 'clone'; (note the lower case) but you have asked to import Clone with an initial upper case letter. I can't test as I don't have that module but perhaps that is your problem.

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11100475]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-25 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found