in reply to Updating 1 array affects the other array

I've tried changing the name of the array but does'nt work.I just ca'nt understand why updating this array will affect another array of a different name

Because a name don't mean much when you're modifying references. Example

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 ' ];
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.

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
    Hi everyone. Thanks for the pointers. I clean up things like removing the arrows. So far it has not work, because there's this part of the code, where I suspect is wrong, but I'm not skilled enough to think of an alternative.
    my %seen_values; my @unique_terminal_id = grep { !($seen_values{$_->[4]}++) } @temp_ope +n_single; my %seen_values; my @unique_manufacturer_model = grep { !($seen_values{$_->[9]}{$_->[10 +]}++) } @unique_terminal_id; my %seen_values; my @unique_manufacturer_model_version = grep { !($seen_values{$_->[9]} +{$_->[10]}{$_->[11]}++) } @unique_terminal_id;
    This portion of code is where I have declared the arrays and just before I encounter the problem. Basically, what I am trying to do is grep unique column pair in a table(array of array) and store them into a new table. I managed to get what I wanted (e.g. the unique pairs), but I ca'nt quite see how it ended up cross-referencing to another array. Thanks!
      ...but I ca'nt quite see how it ended up cross-referencing to another array.
      Let me repeat, it is because you are storing references. If you want copies, use Storable qw(dclone); Observe
      my $ref = [ 1,2]; my $another = [3,4]; my @foo = ( $ref, $another); my @bar = $foo[1]; die $bar[0][1]; __END__ 4 at - line 6.
      get it?