On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> 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?
Not sure if I'd call this more elegant, but perhaps
my $index = 0;
foreach (@myarray) {
...
push(@newarray[$index], <somevalue>);
...
$index++;
}
--
Dan Boger
[EMAIL PROTECTED]
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm