----- Original Message ----- From: "Rod Za" <[EMAIL PROTECTED]> Newsgroups: perl.beginners To: <[EMAIL PROTECTED]> Sent: Friday, July 16, 2004 10:58 AM Subject: Is the var recreated in a foreach loop using `my`?
> 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? > > e.g.: > _BEGIN_ > #!/usr/bin/perl > use warnings; > use strict; > my @array1 =(1,2,3,4,5,6,7,8,9); > foreach(@array1){ > my @array2 = ($_,'a','b','c','d','e'); #is a new the array reated here? or is the same array? > print "$_ = @array2\n"; > } Very clear answer in the docs. perldoc perldsc Read the section 'common mistakes', where this is discussed. (A piece from that document, below) Surprisingly, the following dangerous-looking construct will actually work out fine: for $i (1..10) { my @array = somefunc($i); $AoA[$i] = [EMAIL PROTECTED]; } Chris -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>