Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

192.168.1.41:8000, Www.xxxcon., %s wwwxxxcon, %s wwwxxxcon, www.xxxcon, xxxxdup, bitlord outgoing port settings, ckowwwxxx, 192.168.1.41:8000/nor-482.html, php.ini "Unable to initialize module"

Links

XODOX
Impressum

#1: sub local variable changes global variable.

Posted on 2011-10-16 01:08:19 by 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 happening
- How can I properly pass a two dimensional array to a sub, without the array in main changing?

Ideas anyone?

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 ] = " ";

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
=============

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#2: Re: sub local variable changes global variable.

Posted on 2011-10-16 01:31:00 by 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-dimensional arrays. And this is what you have here. Armed with this
knowledge, perhaps you can already see the problem and solution.

> 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?

When call try(), (or more accurately, when you assign to @b) you are
performing a shallow copy of the array. It seems that you really need a deep
copy. You could either explicity write this yourself, or you could use
something such as dclone in Storable.

> 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 ] = " ";
>
> 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
> =============

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#3: Re: sub local variable changes global variable.

Posted on 2011-10-16 01:44:02 by 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 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?
>
> 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 ] = " ";
>
> 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
> =============

Naming your variables just a and b is a bad idea - the name should
reflect what they contain. Also it is generally better to pass arrays
and hashes to subroutines by reference, but I will work with what you
have written so far as, depending on the problem you are solving, any
changes may be irrelevant.

Your "two-dimensional array" @a is an array of array references, so
calling the subroutine as

try(@a);

in fact passes each row as a separate parameter, like this

try($row0, $row1, $row2, $row3);

and since your subroutine starts with

sub try {
my @b = @_;
:
}

you are simply copying those array references into @b, like this

my @b = ($row0, $row1, $row2, $row3);

so $a[0][1] is the same as $row0->[1] and so also $b[0][1], so by
changing one you are also changing the other. For the local array to be
independent of the one passed, you must duplicate all the rows,
replacing the first line of the subroutine with something like

sub try {
my @b;
foreach my $row (@_) {
push @b, [@$row];
}
:
}


I think it is unlikely that your design is the best solution to your
problem, but since you say nothing of your requirements I cannot suggest
anything better. Depending on the size of your arrays and the level of
recursion you may easily absorb large amounts of memory.

I hope this helps,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#4: Re: sub local variable changes global variable.

Posted on 2011-10-16 04:05:08 by 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 module that is installed with Perl. For a list of
all standard pragmatics and modules, see `perldoc perlmodlib`.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#5: Re: sub local variable changes global variable.

Posted on 2011-10-16 08:29:24 by jwkrahn

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#6: Re: sub local variable changes global variable.

Posted on 2011-10-16 11:31:37 by 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#7: Re: sub local variable changes global variable.

Posted on 2011-10-16 20:10:06 by 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 array containing 16 elements with 16-character strings and doing substr()
- instead of passing a 2 dimensional (16x16) array passing 1-character strings and doing dclone
Every pass I only change a single character, then I run some tests and so on.

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( \@_ ) };
> ...
> }
>
> Storable is a standard module that is installed with Perl. For a list of all standard pragmatics and modules, see `perldoc perlmodlib`.
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#8: Re: sub local variable changes global variable.

Posted on 2011-10-17 00:18:17 by 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( \@_ ) };
>> ...
>> }
>>
>> Storable is a standard module that is installed with Perl. For a list
>> of all standard pragmatics and modules, see `perldoc perlmodlib`.
>
> 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 array containing 16 elements with
> 16-character strings and doing substr()
> - instead of passing a 2 dimensional (16x16) array passing 1-character
> strings and doing dclone
> Every pass I only change a single character, then I run some tests and
> so on.

I suggest you try a simplified version of my code, which is bound to be
faster than calling dclone:

sub try {
my @b = map [@$_], @_;
:
}

Whether it is fast enough only you can tell.

I would also suggest an intermediate solution, where you have a
sixteen-element array, each of sixteen characters. Rather than using
substr you could look at unpack, or possibly split and join.

It is hard to help you further without knowing more about the problem
you are solving.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#9: Re: sub local variable changes global variable.

Posted on 2011-10-17 00:42:26 by 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 code exits?

my @data = (...);

sub try {
my $old = $data[0][1];
$data[0][1] = '7';
...
$data[0][1] = $old;
return 3;
}

Only remembering a single scalar will be many times more efficient than
remembering the entire 256 scalars in the array.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#10: Re: sub local variable changes global variable.

Posted on 2011-10-17 20:31:16 by 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 "@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?

Your problem fascinates me. Please would you help us to understand the
purpose of your program?

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message