Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: deep copy of hash of hash

by haukex (Archbishop)
on May 24, 2019 at 16:57 UTC ( [id://11100476]=note: print w/replies, xml ) Need Help??


in reply to deep copy of hash of hash

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.

Replies are listed 'Best First'.
Re^2: deep copy of hash of hash
by cmic (Acolyte) on May 27, 2019 at 09:25 UTC
    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: note [id://11100476]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found