On Thu, Aug 17, 2000 at 08:46:53PM -0000, Perl6 RFC Librarian wrote:
> This and other RFCs are available on the web at
> http://dev.perl.org/rfc/
>
> =head1 TITLE
>
> Implicit counter in for statements, possibly $#.
Having read over the entire discussion, I want to make a few comments
and throw one major monkey wrench.
The discussion of why the naive
my $i = 0;
foreach my $var ( @list ) {
$i++;
...action...
}
sometimes gives `incorrect' values for $i is tremendous and should be
added to the proposal as justification.
I am *strongly* opposed to creating another line noise variable for
this (speaking as the author of one of the `line noise variables bad'
RFCs). If a default var is to be created (and I think it should be)
it should have a meaningful name like `$loopindex'. But then we get
the wonderful problem of nested loops, which implies some syntax
like
foreach $a ( @alist ) {
foreach $b ( @blist ) {
...do something on matrix $m[$loopindex_a][$loopindex_b];
}
}
This is messy, and the idea of a default index var name probably falls
apart.
The proposed syntaxes for declaring the index on the line are good, but
defeat a feature I'll be pushing for In My Copious Spare Time,
multi-assigns in loops. Many times I'd like to do
foreach my ( $left, $center, $right ) ( @list ) {
....
}
such that #{left,center,right} are assigned the first *three* elements
of the list. (Yes, I know you can do this with data structures.
TMTOWTDI. I want one more.) I use this construct in tcl all the time,
and it's a huge timesaver for well-formatted lists of lists.
Therefore I'd like to see the resulting syntax be as follows:
Frist, preserve current code so that
foreach $scalar_listval ( @list )
works as current.
Second, allow for auto-created indexs by placing a second scalar
following the first
foreach scalar_listval @scalar_index ( @list )
such that the value from list is assigned to scalar_listval and the
variable scalar_index is the index into the list. This has the
advantage that it is currently a syntax error in perl, so no working
code is broken by this addition.
Third, allow for lists of scalars to be placed in the listval position
such that
foreach ( $a, $b, $c ) $i ( @list ) {
...
print $i;
}
such that on each pass through the loop 3 entries are consumed by the
assignment to $a, $b, and $c, and that $i is set to the index value
of the first element. Thus $i would print out as 0, 3, 6, etc. This
allows it to be used to index into @list cleanly should one want to
pick out things like predecessor, etc.
To tell the truth, this third item should probably should become
a separate RFC, and if you'd like to simply say one is forthcoming,
that'd be fine by me.