Hello all Perl monks - I am trying to copy an array of arrays. It is somehow still using the references. I want to alter the copy but leave the original intact. So far I have had very little success. Here is a little sample, can anyone tell me what I am doing wrong. I am about ready to pull my hair out. :-) I know it has to be something simple (or maybe I hope so).
#!/usr/local/bin/perl my @myarray = ([(1..7)],[(1..7)]); my $myarrayref = \@myarray; my @newArray = ( 0, @{$myarrayref}[1..$#{$myarrayref}]); for my $len (0..$#{$myarrayref}) { print "PRE Alteration: @{$$myarrayref[$len]}\n"; } alt_ref($myarrayref); for my $len (0..$#{$myarrayref}) { print "POST Alteration: @{$$myarrayref[$len]}\n"; } sub alt_ref { my ($arr_ref )= @_; my @new_arr = @{$arr_ref}; for my $i (0..$#new_arr) { pop(@{$new_arr[$i]}); } for my $len (0..$#new_arr) { print "DURING Alteration: @{$new_arr[$len]}\n"; } }
The OUTPUT is such:
PRE Alteration: 1 2 3 4 5 6 7
PRE Alteration: 1 2 3 4 5 6 7
DURING Alteration: 1 2 3 4 5 6
DURING Alteration: 1 2 3 4 5 6
POST Alteration: 1 2 3 4 5 6
POST Alteration: 1 2 3 4 5 6
I want the POST Alteration lines to be the same as the PRE Alteration lines(thus leaving the original unaltered.
Thanks in advance.
Mike
In reply to using arrays of arrays by Perl_Wannabe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |