Re: sub local variable changes global variable.

2011-10-17 Thread Rob Dixon
On 16/10/2011 00:08, JPH wrote: 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

Re: sub local variable changes global variable.

2011-10-16 Thread Rob Dixon
On 16/10/2011 23:18, Rob Dixon wrote: On 16/10/2011 19:10, JPH wrote: Every pass I only change a single character, then I run some tests and so on. I wonder if it is possible to write your subroutine by using a single global array and remembering the change so that you can undo it before the

Re: sub local variable changes global variable.

2011-10-16 Thread Rob Dixon
On 16/10/2011 19:10, JPH wrote: On 10/16/2011 04:05 AM, Shawn H Corey wrote: On 11-10-15 07:44 PM, Rob Dixon wrote: sub try { my @b; foreach my $row (@_) { push @b, [@$row]; } : } Or you could use dclone() from Storable: use Storable qw( dclone ); sub try { my @b = @{ dclone( \@_ ) }; ..

Re: sub local variable changes global variable.

2011-10-16 Thread JPH
Thanks! My script seems to work with dclone. Though the (deep) recursion takes a lot of time ... What is your opinion, would it pay off (in performance) when I rewrite the script in such a way that I do not use dclone, but string manipulation routines instead? So: - passing a single dimension

Re: sub local variable changes global variable.

2011-10-16 Thread JPH
You got me there, John. Indeed I do expect it to contain ' ' and not 0 like I stated. If you originally assign " " to $a[ 0 ][ 1 ] why would you now expect it to contain 0? John -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.

Re: sub local variable changes global variable.

2011-10-15 Thread John W. Krahn
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 "

Re: sub local variable changes global variable.

2011-10-15 Thread Shawn H Corey
On 11-10-15 07:44 PM, Rob Dixon wrote: sub try { my @b; foreach my $row (@_) { push @b, [@$row]; } : } Or you could use dclone() from Storable: use Storable qw( dclone ); sub try { my @b = @{ dclone( \@_ ) }; ... } Storable is a standard modul

Re: sub local variable changes global variable.

2011-10-15 Thread Rob Dixon
On 16/10/2011 00:08, JPH wrote: Hi all, 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

Re: sub local variable changes global variable.

2011-10-15 Thread Paul Johnson
On Sun, Oct 16, 2011 at 01:08:19AM +0200, JPH wrote: > Hi all, > > I am passin a two-dimensional array to a sub and when the sub This is your main problem. Perl doesn't have two-dimensional arrays. What it does have is arrays of array references which, if you squint, can be used as two-dimensio

sub local variable changes global variable.

2011-10-15 Thread JPH
Hi all, 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 hap