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

Implicit current-index variable, scoped inside for-loops

Thread Next
From:
Carl Mäsak
Date:
August 28, 2006 23:33
Subject:
Implicit current-index variable, scoped inside for-loops
Message ID:
16d769b70608282333k66326b08kbcb8b2d646f24f04@mail.gmail.com
<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
here.)

Questions:

- 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?

See

 <http://colabti.de/irclogger/irclogger_log/perl6?date=2006-08-29,Tue&sel=145#l205>

for the #perl6 discussion.

-- 
masak

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About