Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Copy of multidimensional hash

by tommaso.fornaciari (Initiate)
on Jan 03, 2013 at 09:19 UTC ( #1011425=note: print w/replies, xml ) Need Help??

in reply to Re: Copy of multidimensional hash
in thread Copy of multidimensional hash

Dear All Monks,
Thanks a lot for your quick and clear answers!
In fact, storing elsewhere the same data, it works:

#!/usr/bin/env perl use Modern::Perl 2011; use autodie; # I create a multidimensional hash my %one = ('a' => [1, 2], 'b' => [3, 4], 'c' => [5, 6]); # I create a copy: # wrong way: #my %two = %one; # right way: my %two; for(keys %one) {$two{$_}[0] = $one{$_}[0]; $two{$_}[1] = $one{$_}[1]} # I print them: say "Before:\n\%one:"; for(sort keys %one){say "$_\t$one{$_}[0]\t$one{$_}[1]"} say '%two:'; for(sort keys %two){say "$_\t$two{$_}[0]\t$two{$_}[1]"} # Then I modify the copy: for(sort keys %two){$two{$_}[1] = 'two'} # And I print again: say "After:\n\%one:"; for(sort keys %one){say "$_\t$one{$_}[0]\t$one{$_}[1]\tOk!!!"} say '%two:'; for(sort keys %two){say "$_\t$two{$_}[0]\t$two{$_}[1]"} # I receive this output: #Before: #%one: #a 1 2 #b 3 4 #c 5 6 #%two: #a 1 2 #b 3 4 #c 5 6 #After: #%one: #a 1 2 Ok!!! #b 3 4 Ok!!! #c 5 6 Ok!!! #%two: #a 1 two #b 3 two #c 5 two

Is this the best way to create a copy of a multidimensional hash?
Thanks again for your helpfulness!

Replies are listed 'Best First'.
Re^3: Copy of multidimensional hash
by muba (Priest) on Jan 03, 2013 at 10:38 UTC
    Is this the best way to create a copy of a multidimensional hash?

    I don't know if it's the best. At any rate, I would reformat the for loop as follows:

    for (keys %one) { $two{$_}[0] = $one{$_}[0]; $two{$_}[1] = $one{$_}[1]; }

    I find this significantly more readable than the way you formatted it. Furthermore, I would generalize copying the arrray, and tell Perl to take out all the elements instead of just those elements with index 0 and 1.

    for my $k (keys %one) { $two{$k}[$_] = $one{$k}[$_] for 0..scalar( @{$one{$k}} ); }

    And then I would generalize it even further: take the whole array reference, dereference it in a single pass, and create and store a new reference to it. Sounds complex? Nah, not so much:

    for my $k (keys %one) { $two{$k} = [ @{$one{$k}} ]; }

    This was already suggested by johngg.</p

    Step by step of [ @{$one{$k}} ]:

    1. $one{$k}: this takes the element identified by key $k from hash %one. That element, in this case, is an array reference.
    2. @{ ... } dereferences the array reference inside the curlies. So after @{ $one{$k} } we're working with a normal, regular, every day array (albeit an anonymous one).
    3. [ ... ] stores whatever is between the square braces as an anonymous array and returns a reference to it.

    But then I would want to generalize it even further and use Storable's dclone function, as suggested by davido.

    use Storable qw(dclone); ... my %two = %{ dclone(\%one) } # dclone() takes a ref and returns a r +ef # So we'll have to put in \%one (not % +one) # and then we have to dereference ( %{ +...} ) # what comes out.

      Thanks muba, thanks johnGG,
      great dissertation about the the topic :))

Re^3: Copy of multidimensional hash
by Neighbour (Friar) on Jan 03, 2013 at 12:27 UTC
    You could use Clone and copy the hash using my %two = %{ Clone::clone(\%one) };
    Or use hashrefs all the way:
    my $hr_one = { 'a' => [1, 2], 'b' => [3, 4], 'c' => [5, 6]}; my $hr_two = Clone::clone($hr_one);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011425]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-25 20:57 GMT
Find Nodes?
    Voting Booth?