in reply to Updating 1 array affects the other array
Because a name don't mean much when you're modifying references. Example
Make a change to $bar[0][0], and you're also changing $bar[1][0] because they are one and the same. It doesn't matter if you rename @bar to @barbar if you still asing $bar[0] and $bar[1] the same reference.my @foo = 1 .. 4; my @bar; my $someReference = \@foo; $bar[0] = $someReference; $bar[1] = \@foo; $bar[2] = $foo[2]; use Data::Dumper; print Dumper( \@bar ),"\n"; $bar[0][0]='change'; $bar[2] .= ' foo '; print " bar[0][0] ", $bar[0][0], "\n"; print " bar[1][0] ", $bar[1][0], "\n"; print Dumper( \@bar ),"\n"; __END__ $VAR1 = [ [ 1, 2, 3, 4 ], $VAR1->[0], 3 ]; bar[0][0] change bar[1][0] change $VAR1 = [ [ 'change', 2, 3, 4 ], $VAR1->[0], '3 foo ' ];
See perldata,perlref,perldsc... and perlmonks own Tutorials
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Updating 1 array affects the other array
by iphony (Acolyte) on Sep 02, 2008 at 08:21 UTC | |
by Anonymous Monk on Sep 02, 2008 at 08:42 UTC |