For nested loops, the index reported would be the innermost loop for the
context:

Let's say the special auto loop index variable is: $^Z

foreach (@array1)
{
        print "Loop1: index=$^Z\n"; #Should report index for loop1
        foreach (@array2)
        {
                print "Loop2: index=$^Z\n"; #Should report index for
loop2
                foreach (@array3)
                {
                print "Loop3: index=$^Z\n"; #Should report index for
loop3
                ...
                }
        }
}

If an user wants to use an outer loop's index inside an inner loop, they
can store it in a variable when in that loop's context.

-Nilanjan


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Ronald J Kimball
Sent: Thursday, September 21, 2006 6:47 PM
To: Palit, Nilanjan
Cc: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

On Thu, Sep 21, 2006 at 09:34:43AM -0700, Palit, Nilanjan wrote:

> I think it'd be fairly easy for Perl to auto initialize & increment a
> loop index in all loops & provide that to the user in a special
> variable. "$." is an excellent example. I think it'd be a great
addition
> to Perl's excellent (& long) list of special vars, making for yet more
> elegant & concise code.

What would you have Perl do in the case of nested loops?

Ronald
 
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm
 
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to