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}} ]:
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.
In reply to Re^3: Copy of multidimensional hash
by muba
in thread Copy of multidimensional hash
by tommaso.fornaciari
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |