JPH wrote:
Hi all,
Hello,
I am passin a two-dimensional array to a sub and when the sub returns, the original array has changed. Eventually I want to pass the array into a recursive sub, so I want to find a way to circumvent this behaviour. Notice how my global is "@a" and the sub local is "@b" - Why is this happening - How can I properly pass a two dimensional array to a sub, without the array in main changing? Ideas anyone?
Change your subroutine so that it does not modify the array values?
Here is the minimal script to reproduce my finding: ============= #!/usr/bin/perl use warnings; use strict; sub try { my @b = @_; $b[ 0 ][ 1 ] = "7"; return 3; } my @a; $a[ 0 ][ 1 ] = " ";
Or more simply: my @a = [ undef, " " ];
print "Before: " . $a[ 0 ][ 1 ] . "\n"; try( @a ); print "After: " . $a[ 0 ][ 1 ] . "\n"; exit; ============= What happens: Before: After: 7 ============= What I would expect: Before: After: 0
If you originally assign " " to $a[ 0 ][ 1 ] why would you now expect it to contain 0?
John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/