Thank you, Bob and Chris Charley,

In the Bob's example:

e.g.
>    for (1 .. 5) {
>        my @arr = ($_) x $_;
>        print [EMAIL PROTECTED], " = @arr\n";
>    }
> 
>    ARRAY(0x8103418) = 1
>    ARRAY(0x80f827c) = 2 2
>    ARRAY(0x8103490) = 3 3 3
>    ARRAY(0x81034cc) = 4 4 4 4
>    ARRAY(0x8103514) = 5 5 5 5 5

how can i access, for example, the array referenced by (0x80f827c)?

Thank you very much,

Rod


--- Bob Showalter <[EMAIL PROTECTED]> wrote:
> Rod Za wrote:
> > Hi,
> > 
> > I have a doubt. If i create an array inside a foreach loop using
> > `my`, is this array recreated every time or every time the loop pass
> > by i got a new array? 
> 
> Yes, it's a new array.
> 
> Try this:
> 
>    for (1 .. 5) {
>        my @arr = ($_) x $_;
>        print [EMAIL PROTECTED], " = @arr\n";
>    }
> 
> Output:
>    ARRAY(0x81017e4) = 1
>    ARRAY(0x81017e4) = 2 2
>    ARRAY(0x81017e4) = 3 3 3
>    ARRAY(0x81017e4) = 4 4 4 4
>    ARRAY(0x81017e4) = 5 5 5 5 5
> 
> The address of the array never changes, so it looks like the same array.
> However, if you save a reference to the array inside the loop, watch what
> happens:
> 
>    my @outer;
>    for (1 .. 5) {
>        my @arr = ($_) x $_;
>        print [EMAIL PROTECTED], " = @arr\n";
>        push @outer, [EMAIL PROTECTED];
>    }
> 
> Output:
>    ARRAY(0x8103418) = 1
>    ARRAY(0x80f827c) = 2 2
>    ARRAY(0x8103490) = 3 3 3
>    ARRAY(0x81034cc) = 4 4 4 4
>    ARRAY(0x8103514) = 5 5 5 5 5
> 
> Here it's proven that you create a new array each time. If you never take a
> reference to the "my" variable that lives beyond the scope of the loop, Perl
> can reuse the old array, and just put new contents into it. I'm not sure if
> down in the guts of perl it's actually releasing and then recreating the
> array or whether it's just reusing the old array. Really doesn't matter; the
> result is you always get a different array and they will be kept separate if
> necessary.
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 



                
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to