[Israel.pm] Can anyone explain my mistake ?

Richie Sevrinsky richie at mamash.com
Wed Jun 10 02:03:51 PDT 2009


perldoc perlsyn:

   Foreach Loops
       The "foreach" loop iterates over a normal list value and sets the 
variable VAR to be each element of the list in turn.  If the variable is 
preceded with the
       keyword "my", then it is lexically scoped, and is therefore 
visible only within the loop.  Otherwise, the variable is implicitly 
local to the loop and regains its
       former value upon exiting the loop.  If the variable was 
previously declared with "my", it uses that variable instead of the 
global one, but it’s still localized
       to the loop.  This implicit localisation occurs only in a 
"foreach" loop.

- Richie

Berler Chanan wrote:
> If I correct the script like this:
> I get this result: I get lv_num = 0 ????? why not 3
> Weird to me....thought it will update the number correctly
> Chanan
>
> my @arr = (1,2,3,4,5);
> my $lv_num = 0;
> foreach $lv_num (@arr)
> {
>    print "Found $lv_num\n";
>    last if ($lv_num == 3);
> }
>
> print "lv_num = $lv_num\n";
>
>
>
> -----Original Message-----
> From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
> Of Berler Chanan
> Sent: Wednesday, June 10, 2009 11:48 AM
> To: 'Perl in Israel'
> Subject: [Israel.pm] Can anyone explain my mistake ?
>
> Hi All
>
> I am running this simple foreach loop:
> why do I get lv_num = <null> ? am I missing something ?
> Thanks
> Chanan
>
>
> my @arr = (1,2,3,4,5);
>
> foreach $lv_num (@arr)
> {
>    print "Found $lv_num\n";
>    last if ($lv_num == 3);
> }
>
> print "lv_num = $lv_num\n";
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.339 / Virus Database: 270.12.56/2161 - Release Date: 06/09/09
> 18:08:00
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>   


More information about the Perl mailing list