in reply to What's missing in Perl 5.10

What is missing is the ability to use each on an array. It would work like this:

my @array = qw( foo bar rat ); while (my ($val,$idx) = each @array) { say "$val at $idx"; } # produces foo at 0 bar at 1 rat at 2

Sometimes it's really nice to have both the value and index at once. Nicholas Clark had this up and running in bleadperl a few months ago, but the pumpking judged that it was regretfully a far too major change in the syntax to hold up the release of 5.10

There is discussion about how to retro-future-fit it into the 5.10 maintenance track, so as to avoid a full release cycle that would require waiting for the first 5.12 release.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: What's missing in Perl 5.10
by Roy Johnson (Monsignor) on Dec 20, 2007 at 21:33 UTC
    That made me want to implement it. Ironically, I don't have 5.10 installed yet, and there are a few features that would be appropriate here. Note that I reversed the result list, so that the order is key/value, like hash-each is.
    use strict; use warnings; BEGIN { my %state; sub a_each(\@) { my $aref = shift; exists $state{$aref} or $state{$aref} = -1; if (++$state{$aref} > $#$aref) { delete $state{$aref}; return (); } else { return ($state{$aref}, $aref->[$state{$aref}]); } } } my @array = qw( foo bar rat ); while (my ($idx,$val) = a_each @array) { print "$val at $idx\n"; }

    Caution: Contents may have been coded under pressure.
Re^2: What's missing in Perl 5.10
by blazar (Canon) on Dec 20, 2007 at 16:34 UTC
    What is missing is the ability to use each on an array.

    I personally believe that it could be worth to remind of ysth's 5.12 feature quest to which I've just added a reply.

    There is discussion about how to retro-future-fit it into the 5.10 maintenance track, so as to avoid a full release cycle that would require waiting for the first 5.12 release.

    But wouldn't the new feature mechanism make it easier to implement new syntactic features without risking too much with backwards compatibility?

    --
    If you can't understand the incipit, then please check the IPB Campaign.
Re^2: What's missing in Perl 5.10
by moritz (Cardinal) on Dec 21, 2007 at 08:16 UTC
    each @array is now implemented in blead (and returns ($index, $value), not the other way round).

    See this thread for details, once nntp.perl.org works again.

    And there is Array::Each::Override, thanks to Nicholas for the hint.