develooper Front page | perl.perl6.language.flow | Postings from September 2000

Re: Final draft of RFC 120: Implicit counter in for statements

Thread Previous
From:
Bart Lateur
Date:
September 18, 2000 19:44
Subject:
Re: Final draft of RFC 120: Implicit counter in for statements
Message ID:
aakdss0g3cg2n3fo9j2os843f760e5lq3p@4ax.com
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.

Thread Previous


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