Consider this
Expand|Select|Wrap|Line Numbers
- use strict;
- use Data::Dumper;
- my %hash1=(a=>[1,1],
- b=>[2,2],
- c=>[3,3],);
- my %hash2=%hash1;
- $hash1{a}[0]=10;
- print Dumper(%hash2);
the SAME arrays in %hash1, so changing an array element in %hash1 changes the respective element
in the new %hash2. But I want to store the old value of %hash1 into %hash2.
In a first attempt to solve that problem, I come with that :
Expand|Select|Wrap|Line Numbers
- use Data::Dumper;
- use strict;
- my %hash1=(a=>[1,1],
- b=>[2,2],
- c=>[3,3],);
- my %hash2=();
- while ( my ($key,$val)= each %hash1) {
- my @arr=@{$val};
- $hash2{$key}=\@arr;
- }
- $hash1{a}[0]=10;
- print Dumper(%hash2);
I've also found that can surround the problem by assigning not a new value to the array (see: $hash1{a}[0]=10; )
but assigning a whole new array like this : $hash1{a}=[10,10];
But I still can't understand why the arrays are "copied" with the same address into %hash2 and
how I can store a %HoA into another %Hoa ...
please help