Andrew Gaffney <[EMAIL PROTECTED]> wrote:
: 
: I have code that uses a 'foreach(@array) {}' to loop
: through an array. I now need to be able to get the
: array index inside of that. I know I could switch to
: a 'for($loopvar=0;$loopvar<@array;$loopvar++) {}' to
: do that, but I'd have to change some other code also.
: If not that way, is there a way to get the array
: index inside of a loop like 'for(0..$#array) {}'?

    You could count the index:
{
    my $index = 0;
    foreach my $value ( @array ) {
          .
          .
          .
        $index++;
    }
}

    The outer braces keep $index to a smaller scope.
BTW, $value above is an alias. If you are needing the
index to change the current value in @array you can do
it by changing $value.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328




-- 
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