RFC 120 (v2) Implicit counter in for statements, possibly $#.

[email protected] (Perl6 RFC Librarian)
Newsgroups perl.perl6.language.flow,perl.perl6.announce
Message-ID <[email protected]>
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Implicit counter in for statements, possibly $#.

=head1 VERSION

  Maintainer: John McNamara <[email protected]>
  Date: 16 Aug 2000
  Last-Modified: 16 Aug 2000
  Version: 2
  Mailing List: [email protected]
  Number: 120

=head1 ABSTRACT

The syntax of Perl style C<for> statements could be simplified by the
introduction of an implicit counter variable. The deprecated variable
C<$#> could be used for this purpose due to its mnemonic association
with C<$#array>.

=head1 DESCRIPTION

The following discussion makes no distinction between the C<for> and
C<foreach> statements.

The use of the C<foreach> statement or the C<for> statement in
conjunction with the range operator, C<..>, are generally seen as good
idiomatic Perl:

    @array = qw(sun moon stars rain);
    
    foreach $item (@array) {
        print $item, "\n";
    }

as opposed to the "endearing attachment to C" style:

    for ($i = 0; $i <= $#array; $i++) {
        print $array[$i], "\n";
    }

However, the latter format is often more convenient if you need to
keep track of the array index as well as iterating over the array:

    for ($i = 0; $i <= $#array; $i++) {
        print $array[$i], " is at index ", $i, "\n";
    }

is more succinct than:

    $i = 0;
    foreach $item (@array) {
        print $item, " is at index ", $i, "\n";
        $i++;
    }

The addition of an implicit counter variable in C<for> statements
would lead to a more elegant syntax. It is proposed that the
deprecated variable C<$#> should be used for this purpose due to its
mnemonic association with C<$#array>. For example:

    foreach $item (@array) {
        print $item, " is at index ", $#, "\n";
    }


The variable C<$#> currently holds the output format for printed
numbers.


=head1 IMPLEMENTATION

Not sure. However, consideration would have to be given to nested
C<for> statements and how the variable would be affected by C<local>.

=head1 REFERENCES

perlvar

Alex Rhomberg proposed an implicit counter variable on clpm:
http://x53.deja.com/getdoc.xp?AN=557218804&fmt=text and
http://x52.deja.com/threadmsg_ct.xp?AN=580369190.1&fmt=text

Craig Berry suggested C<$#>:
http://x52.deja.com/threadmsg_ct.xp?AN=580403316.1&fmt=text
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.