[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] That was productive...



Stas Bekman wrote:
> > Hmm. I wonder how many ex-C now perl programmers have written...
> >    for( $i = 0; $i < $#array; $i++)
> > instead of...
> >    for( $i = 0; $i <= $#array; $i++)
> 
> The Perl idiomatic syntax is:
>     for( $i = 0; $i < @array; $i++)

I wouldn't say so -- I thought "standard" (idiomatic) Perl was the second
statement you quoted (with <= $#array), or else

   foreach(@array) { ... }

. It seems to me that if you're using indices (indexes?) to loop through the
array from 0 to whatever, you use the construct that gives you the last
index. The size of the array is then unimportant for the moment as you're
concerned with indices.

This might be different if you're coming from C and used to using sizeof()
or strlen()... then you're working with sizes. But then C has no way of
telling you the index of the last element of an array.

Cheers,
Philip

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe