On Mon, 18 Sep 2000 23:11:28 +0100, John McNamara wrote:

>     foreach $item (@array) {
>         print $item, " is at index ", $#, "\n";
>     }

Maybe I'm a little late... But I just thought how neat it would be if
this would also extend to map() and grep().

Remember, officially the processing order of the items for map() and
grep() isn't layed down in stone. So an independent running counter with
an external variable ($i++) is a bit of a no-no. This loop counter
would, without any doubt, tell you what item number you've got now.

For example, a stable sort (original order if items are equal) using a
Schwartzian Transform (it's in the 3rd line):

        @stablesort = map { $_->[0] }
          sort { $a->[0] cmp $b->[0] || $a_->[1] <=> $b->[1] }
            map { [ $_, $# ] } LIST_OF_STRINGS;


Er, provided that stable sort wasn't going to be part of Perl6, that is.

-- 
        Bart.

Reply via email to