Perl_Wannabe has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using arrays of arrays
by kcott (Archbishop) on Nov 05, 2010 at 06:15 UTC | |
|
Re: using arrays of arrays
by ikegami (Patriarch) on Nov 04, 2010 at 22:56 UTC | |
|
Re: using arrays of arrays
by CountZero (Bishop) on Nov 05, 2010 at 00:43 UTC | |
|
Re: using arrays of arrays
by chrestomanci (Priest) on Nov 05, 2010 at 11:48 UTC | |
|
Re: using arrays of arrays
by locked_user sundialsvc4 (Abbot) on Nov 05, 2010 at 11:39 UTC | |
|
Re: using arrays of arrays
by JavaFan (Canon) on Nov 05, 2010 at 11:54 UTC |