In Perl, an array of arrays is really an array of references to arrays. You can see that by printing @AoA1:
say @AoA1; # ARRAY(0x606df0)ARRAY(0x621e70)
When you
push @AoA2, @AoA1; you're pushing the same references on to @AoA2:
say @AoA2; # ARRAY(0x606df0)ARRAY(0x621e70)
The hex numbers are the same, indicating the both refer to the same area of memory.
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
- the freeze & thaw functions from the core module Storable
- the CPAN module Clone
- some Perl
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)
Storable and
Clone will work with any data structure but the pure perl method will need to be tailored to each specific data structure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.