http://www.perlmonks.org?node_id=1011436


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

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.