in reply to Re^2: accessing element of array of arrays
in thread accessing element of array of arrays
When you push @AoA2, @AoA1; you're pushing the same references on to @AoA2:say @AoA1; # ARRAY(0x606df0)ARRAY(0x621e70)
The hex numbers are the same, indicating the both refer to the same area of memory.say @AoA2; # ARRAY(0x606df0)ARRAY(0x621e70)
So, to answer your original question, changing one of the array elements, will also change the corresponding element in the other array. To get round that, you could use
Storable and Clone will work with any data structure but the pure perl method will need to be tailored to each specific data structure.use Clone 'clone'; my @AoA3 = @{ clone( \@AoA1 ) }; say @AoA3; # ARRAY(0x7c46c0)ARRAY(0x7c4720) use Storable qw/freeze thaw/; my @AoA4 = @{ thaw( freeze( \@AoA1 ) ) }; say @AoA4; # ARRAY(0x7095c8)ARRAY(0x709718) my @AoA5 = map { [ @$_ ] } @AoA1; say @AoA5; # ARRAY(0x67ff58)ARRAY(0x7e8468)
|
|---|