Re: RFC 262 (v1) Index Attribute
Webmaster wrote: (I have attached a prototype of what I had in mind) From: "David L. Nicol" [EMAIL PROTECTED] Yes, that is exactly what is being suggested, but the "indexof" function is implicit in the attribute. Your code becomes print "Found It at position ${_:n}!\n" if /$seek/ foreach @items I do like the iterator, but I also prefer: " split( /$delim/, $items[$index] );" To " split( /$delim/, $items[${_:n}]);" for readability. yes. I imagine :n would be used in debugging code, where you don't really need it for the general meaning of the passage. $items[$_:n]# curlies only are required for interpolation I didn't say it so many words, but an example of how to _access_ an attribute inside interpolation -- that is what I was getting at too; The two might actually be able to work together, if ':n' was an element of the actual array, whereby a subsequent call to the array index function using 'null' or 'undef' as the starting index would pick up where it left off. The other part is that the array index function could potentially return a list. (see attached) It's only a suggestion, and I have programmed long enough to do it other ways (see attached), but if RFC 199 doesn't fly, this would be nice to have around. Grant M. [EMAIL PROTECTED] it's not a member, but an attribute, thus the colon. The curlies are only needed because we're inside double-quotes in my example.
Re: RFC 262 (v1) Index Attribute
David L. Nicol wrote: print "Found It at position ${_:n}!\n" if /$seek/ foreach @items If we are going to be throwing around attributes like that, why don't we switch to using the ubiquitously recognizable dot instead of colon? for my $iter ( @things ) { print "index= ", $iter.indexof, "\n"; -- John Porter Jetzt schalten wir das Radio an. Aus dem Lautsprecher klingt es dann...
Re: RFC 262 (v1) Index Attribute
John Porter wrote: David L. Nicol wrote: print "Found It at position ${_:n}!\n" if /$seek/ foreach @items If we are going to be throwing around attributes like that, why don't we switch to using the ubiquitously recognizable dot instead of colon? for my $iter ( @things ) { print "index= ", $iter.indexof, "\n"; I hope that was a rhetorical question, but here is the rhetorical answer: Because the ubiquitously recognizable dot is already in use as the string concat operator in this language, and overloading it would cause far far far too much ambiguity. This answer also goes for schemes for optimising - into one character.
Re: RFC 262 (v1) Index Attribute
Webmaster wrote: What would really be nice here is an Cindex function, similar to the scalar version, that returns the index of the matching entry in a list. For instance: my $n=0; foreach (@items){ print "Found It at position $n!\n" if /$seek/; $n++; } Could be replaced by: if (my $n = arrindex( @items, $seek )) { print "Found It at position $n!\n" ; } Grant M. [EMAIL PROTECTED] Yes, that is exactly what is being suggested, but the "indexof" function is implicit in the attribute. Your code becomes print "Found It at position ${_:n}!\n" if /$seek/ foreach @items -- David Nicol 816.235.1187 [EMAIL PROTECTED] "A taste so good that we stand behind every bottle and can."
Re: RFC 262 (v1) Index Attribute
David Nicol Wrote in RFC 262: foreach $item (@items){ #print "$item was at location ",$item:n,"\n"; print "$item was at location ${item:n}\n"; }; What would really be nice here is an Cindex function, similar to the scalar version, that returns the index of the matching entry in a list. For instance: my $n=0; foreach (@items){ print "Found It at position $n!\n" if /$seek/; $n++; } Could be replaced by: if (my $n = arrindex( @items, $seek )) { print "Found It at position $n!\n" ; } Grant M. [EMAIL PROTECTED]
Re: RFC 262 (v1) Index Attribute
On Wed, Sep 20, 2000 at 08:05:20AM -0400, Webmaster wrote: David Nicol Wrote in RFC 262: foreach $item (@items){ #print "$item was at location ",$item:n,"\n"; print "$item was at location ${item:n}\n"; }; What would really be nice here is an Cindex function, similar to the scalar version, that returns the index of the matching entry in a list. For instance: my $n=0; foreach (@items){ print "Found It at position $n!\n" if /$seek/; $n++; } Could be replaced by: if (my $n = arrindex( @items, $seek )) { print "Found It at position $n!\n" ; } Well if there ever is a way to shortcut grep this could be genera;ized to my $index = grep { break if $_ eq $seek; 1 } @items; assuming `break' causes the shortcut An therefore allowing more comparisons than just eq Graham.
Re: RFC 262 (v1) Index Attribute
Graham Barr Wrote: Well if there ever is a way to shortcut grep this could be genera;ized to my $index = grep { break if $_ eq $seek; 1 } @items; Wouldn't this also assume that grep return the number of times the block was NOT true, rather than it's current implementation of the number of times it IS true? I think that in the example, grep would simply return either '1' or '0', unless there was some easy way to say: my $index = grep { while( $_ ne $seek) } @items; I might be wrong here, though. It's still early. Grant M. [EMAIL PROTECTED]
Re: RFC 262 (v1) Index Attribute
On Wed, Sep 20, 2000 at 09:03:39AM -0400, Webmaster wrote: Graham Barr Wrote: Well if there ever is a way to shortcut grep this could be genera;ized to my $index = grep { break if $_ eq $seek; 1 } @items; Wouldn't this also assume that grep return the number of times the block was NOT true, But in that example the block is always true, thats what the 1 is for. Graham.
RFC 262 (v1) Index Attribute
This and other RFCs are available on the web at http://dev.perl.org/rfc/ =head1 TITLE Index Attribute =head1 VERSION Maintainer: David Nicol [EMAIL PROTECTED] Date: 19 Sep 2000 Mailing List: [EMAIL PROTECTED] Number: 262 Version: 1 Status: Developing =head1 ABSTRACT An attribute C:n is defined which means the index or key of a contained item. This attribute is useful when an index number is needed within a Cforeach statement, it also may be used inside container-like abstractions which dynamicly generate their contents. =head1 DESCRIPTION foreach $item (@items){ #print "$item was at location ",$item:n,"\n"; print "$item was at location ${item:n}\n"; }; Given acceptance of several other iffy proposals, @Nplus1 = lazy sub { return $_:1 + 1 }; # or something like this =head1 DISCUSSION When this was suggested on the language-objects list, there was discussion of the name of the attribute, with C :i being suggested as well as C :index We also need access to attributes of variables as a standard language feature, that is implicit here. Another possibility would be to use C $[ for the attribute, which makes sense if we allow C $[ to be set individually per container. Use of brackets in variable names is a Big Hassle for parsing: print "$item was at location ${item:[}\n"; is parseable if colon is elevated to the same level of immediacy, in terms of eating subsequent tokens, as dollar-sign now has. Use of left-bracket as the index attribute matches very well with the situation of pulling sequential items out of a function pretending to be a list, though, as internal to the code we can refer to $[, "The index of the first item", to get the index of the current item after several items have been shifted off (and the specific $[ attribute incremented each time.) @Nplus1 = lazy sub { return $[ + 1 }; # or something like this =head1 IMPLEMENTATION Within looping constructs, we only need to track this if it is used. We may be using an implied c-like Cfor when we do a looping construct anyway, in which case we already have the information, and just need some Perl syntax for sharing it with user code. In linked-list kinds of situations, we will need to identify the need to track this info and then track it for the user. In terms of $[ we would make the familiar and vaguely deprecable $[ be the global to which the one specific to an array instance defaults to or starts at. (which may deserve it's own RFC it is true.) =head1 REFERENCES John McNamara left this radical suggestion out of RFC 120.