in reply to Re: Automatic Loop Counter not in perl
in thread Automatic Loop Counter not in perl

Yes, Perl 6's solution is rather generic.

A loop counter can easily be done like this:

for @array Z (0..*) -> $item, $count { say "$item occured at position $count"; }

An array is (lazily) zipped with a (lazy) infinite array (1, 2, 3 Z 'a', 'b', 'c' evaluates to (1, 'a'), (2, 'b', (3, 'c'), in list context it is flattened (1, 'a', 2, 'b', 3, 'c') and the two loop variables eat up two elements per iteration.

Note that

Hooray for Perl 6!