[Apologies for the late reply. Still catching up]

On Thu, 17 Aug 2000 20:51:01 -0500, David L. Nicol said:
>  What if its a method of anything in an array?  $_ is already
>  a reference to the object on the array in for loops rather
>  than a copy of it.  What if we make change be not something about
>  for loops, but about anything in an array?
>  
>       print "The index, in its array, of <<$_>> is $CORE::ARRAY_INDEX{$_}"
>  
>  where %CORE::ARRAY_INDEX is a very magical system-provided hash that
>  tells us the index in its array of something that is in an array.  It
>  is often undefined; and would get tricky but definable for objects that
>  can be in multiple containers at once, I'd think it would be the index
>  of the item in the most recent container it was accessed from.
>  
>  If we are going to have arrays that can be sparse, we've pretty much got
>  to keep track of this info somehow anyway, so might as well give a way
>  to access it.

What's wrong with defining each() on arrays?

  while(my($i,$v)=each @array){
    print "Element at $i is $v\n";
  }

Then if you have a sparse array, you only get the defined elements, otherwise
you get all of them.

Whether for iterates over the defined elements of a sparse array, or all of
them is another question. If that gets optimized into an iterator, you're
only likely to get the defined elements, because that'll probably use the
same mechanism as each(). I suppose there could be two mechanisms, then
implementors could choose whether to make them do the same or different
things. Otherwise, there should be some way of specifying that you want all
elements (or only defined, whatever the default isn't) when creating an
iterator or using a for loop on an array.

  # This is the easy bit
  my $iter1=$array->iter_all;
  my $iter2=$array->iter_def;
  my $iter3=$array->iter; # Default to iter_def ?

  # This isn't
  for(@$array){} # default iterator
  for($array->iter_all){} # Change for to call iterators?
  for($array->iter_def){}

I suppose this also gives you the choice with each():

  while(my($i,$v)=each @$array){} # Default iterator
  while(my($i,$v)=each $array->iter_all){} # All elements
  while(my($i,$v)=each $array->iter_def){} # Defined elements

This is looking like for/each should act on iterators "natively", and create
an iterator if given a list/hash.

Reply via email to