duyet has asked for the wisdom of the Perl Monks concerning the following question:
I have a problem with ref array which i have spent sometimes debugging it. I fixed it but still don't understand how this can happen. Below is a shorten version of the code:
After running the code:my $ra = [ 'A', 'B', 'C' ]; print STDERR '1. ra = ' . Dumper( $ra ); test_ra( $ra ); print STDERR '2. ra = ' . Dumper( $ra ); exit; sub test_ra { my $data = shift; foreach my $item ( @{ $data } ) { $item = 'Z'; } }
1. ra = [ 'A','B','C' ] 2. ra = [ 'Z','Z','Z' ]
Apparently assigning some other value to $item modifies my ref array. This is something i'm not expecting. Off course when i copy the content of the array to another one (my @dup_array = @{$data}) and use it, the problem will disappeared.
Maybe i've overlooked something, but hopefully you can shine some light on the darkness.
TIA
PS: i'm using v5.8.8
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ref array being modified unexpectively
by davido (Cardinal) on Jul 29, 2011 at 08:52 UTC | |
|
Re: ref array being modified unexpectively
by moritz (Cardinal) on Jul 29, 2011 at 08:52 UTC |