On Thu, 17 Aug 2000, John McNamara wrote:

> On Wed, Aug 16, 2000 at 11:42:20AM -1000, Tim Jenness wrote:
>  > What about:
>  >
>  > for (0..$#array) {
>  > print $array[$i], " is at index ", $i, "\n";
>  > }
>  >
>  > I use that whenever I need to loop over indices of two arrays at once.
> 
> 
> I use it myself ;)
> 
> 
> Iterating over an array in terms of the array variable instead of the array 
> index provides a useful level of abstraction in a for/foreach loop. It adds 
> a certain beauty to a programming language to have a for loop without the 
> need to define a lower bound, an upper bound and a counter.
> 

Just to add, I like the proposal to provide access to the array element
and the index. It was just that your example was far more verbose than was
required (using C style for) -- it felt like you were trying to make the
case stronger by starting from as complicated an example as possible :-)

I often find myself wishing that

  for (@array) {

  }

could give me the index as well as the element.

  for my($element,$index) (@array) {

  }

is a good alternative (with $_ and $# being the implict forms).


-- 
Tim Jenness
JCMT software engineer/Support scientist
http://www.jach.hawaii.edu/~timj


Reply via email to