Forgot to add: The second alternative is also wordy:

foreach (0 .. $#myarray)
{
...
push(@newarray[$_], <somevalue>);
...
}

And just like the for-loop, I longer have the array element in $_, which
makes all implicit $_ operations (regex's, chomp's, ...) wordier ...

-Nilanjan


> _____________________________________________ 
> From:         Palit, Nilanjan  
> Sent: Monday, September 18, 2006 10:31 AM
> To:   L-boston-pm
> Subject:      Loop index in foreach?
> 
> In a foreach loop, is there a way to find out the loop index number?
> E.g.:
> 
> foreach (@myarray)
> {
> ...
> push(@newarray[<??loopindex??>], <somevalue>);
> ...
> }
> 
> Currently, I have to resort to the following:
> for (my $i= 0; $i <= $#myarray, $i++)
> { ... push(@newarray[$i], <somevalue>); ...}
> 
>  ... which is more wordy -- I was wondering if there's a more
> elegant/efficient (i.e. less code) to do this?
> Thanks,
> 
> -Nilanjan
> 
> 
> Nilanjan Palit
> __________________________________________
> Infrastructure Processor Division, CIG/DEG
> Intel Massachusetts Development Center
> Email: [EMAIL PROTECTED]
> 
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to