{}think has asked for the wisdom of the Perl Monks concerning the following question:
This question regards reusing an array as I spin through a loop, and creating references to that array inside the loop. My intuition tells me this can be fraught with unintentional side effects, and I have designed a solution that works. I have also created a snippet that does things the 'wrong' way. So I've got my solution and should be happy... problem is, I don't understand what the bad code is doing, and worse, I don't get WHY the good code works! Intuition has served me well, but now I seek knowledge!!!
My goal is to put information in an array "@a" every time I iterate through a loop, and then push a reference to that array onto an outside array called "@container".
I want to clear out @a each time. My concern is that if I do this wrong, all the entries in @container will point to the same location in memory, which will be a nasty mishmash.
So here's the technique that I knew was wrong:
#!/usr/bin/perl -w use strict; use Data::Dumper; #---- The wrong way my @container=(); my @a=(); for (my $i=0; $i <=1; $i++){ @a=(); $a[$i] = 99; push @container, \@a; } print Dumper(\@container);
And here is the output:
$VAR1 = [
[
undef,
99
],
$VAR1->[0]
];
Problem 1: I don't really understand why it resulted in that.
So, here is the solution that I designed which does work the right way. This version differs in that it declares the array inside of the scope of the loop. It works.
#!/usr/bin/perl -w use strict; use Data::Dumper; #By using careful scoping, you can control allocation of an array that + you intend to reuse. #---- The right way { my @container=(); for (my $i=0; $i <=1; $i++){ my @a=(); #@a is declared INSIDE the scope of the lo +op $a[$i] = 99; push @container, \@a; } print Dumper(\@container); }
The output is as I desire. There are two arrays, and their contents are independent of on another. The first array has its first element set to 99, and the second array has its second element (but not its first element) set to 99. Problem #2: I don't understand why my solution works!
$VAR1 = [
[
99
],
[
undef,
99
]
];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scope and references
by BrowserUk (Patriarch) on Jun 19, 2011 at 15:59 UTC | |
by {}think (Sexton) on Jun 19, 2011 at 16:52 UTC | |
by BrowserUk (Patriarch) on Jun 19, 2011 at 17:41 UTC | |
by {}think (Sexton) on Jun 19, 2011 at 18:44 UTC | |
by {}think (Sexton) on Jun 22, 2011 at 02:33 UTC | |
by Anonymous Monk on Jun 23, 2011 at 13:05 UTC | |
by ikegami (Patriarch) on Jun 19, 2011 at 19:35 UTC | |
by 7stud (Deacon) on Jun 20, 2011 at 08:01 UTC | |
by ikegami (Patriarch) on Jun 20, 2011 at 17:03 UTC | |
by jpl (Monk) on Jun 19, 2011 at 18:31 UTC | |
by jpl (Monk) on Jun 20, 2011 at 10:51 UTC | |
|
Re: Scope and references
by AnomalousMonk (Archbishop) on Jun 19, 2011 at 19:59 UTC | |
|
Re: Scope and references
by 7stud (Deacon) on Jun 19, 2011 at 20:59 UTC | |
by ikegami (Patriarch) on Jun 20, 2011 at 03:48 UTC | |
by 7stud (Deacon) on Jun 20, 2011 at 08:15 UTC | |
by {}think (Sexton) on Jun 20, 2011 at 00:03 UTC |