The section of Apocalypse 2 'Other Decisions About Variables' states:
"$#foo is gone. If you want the final subscript of an array, and [-1] isn't
good enough, use @foo.end instead."

Here is an example where -1 is not good enough:

# this perl 5 code...
foreach $index (0..$#array) {
    do_something($index, $array[$index]);
}

# becomes this perl 6 code
foreach $index ([EMAIL PROTECTED]) {
    do_something($index, @array[$index]);
}

The paticular use of $# above got me thinking... Is there any reason that
'keys' (and 'each' and 'values' for that matter) couldn't also "do the right
thing" if given an array instead of a hash?

'keys' would return a list of the index numbers equivalent to (0..$#array).
'each' would return an index number, value pair.
'values' would return a list of the values in the array (for consistency).
(since we're dealing with an array, the results would be returned in array
order.)

# proposed
foreach $index (keys @array) {
    do_something($index, @array[$index]);
}



Reply via email to