develooper Front page | perl.perl6.language | Postings from August 2006

Implicit current-index variable, scoped inside for-loops

Thread Next
Carl Mäsak
August 28, 2006 23:33
Implicit current-index variable, scoped inside for-loops
Message ID:
<Yobert> Hey do you know what would be cool in perl 6
<Yobert> A special variable for when you do a for (@array) style loop
<Yobert> it would always have the index of the array

Discussed on #perl6: it's already quite easy in Perl 6 to loop with an
explicit index:

my @array = <moose elk caribou>;
for @array.kv -> $i, $val {
  say "$i\t$val";

But maybe a variable that implicitly carries along the loop index
would be even snazzier?

for @array -> $val {
  say "$.\t$val";

(Change "$." to whatever name would actually be appropriate in this
case. "$." contains the current line number for the last filehandle
accessed in Perl 5, and that's probably why I came to think of it


- Is the itch big enough for this? The more I look at the first piece
of code, the more I'm thinking "that's not so bad, really". (Though
opinions differed on the IRC channel.) Is there a situation I'm not
thinking of where the first piece of code would suck, and the second
piece of code would rock? Or is this a case of oversugaring?

- I feel there's a trend of moving away from line-noise variables. I'd
hate to be one to propose adding a new one to the language. Is there a
better syntax than "$."?

- How would this work with non-array data? Specifically, what happens
with next, redo etc on a filehandle, for example?



for the #perl6 discussion.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About