in reply to How do I record in what order a sort was preformed

Here's another way. Might not be as good as the examples above, but in the spirit of TMTOWTDI...

my $count = 0; my %hash = map {$_, $count++} @array; my @sort = sort { int($a) <=> int($b) } @array; my $flag = 0; for (@sort) { print ', ' if $flag++; print qq|"$_" came from | . '$array[' . $hash{$_} . ']'; }

--Dave

Replies are listed 'Best First'.
Re: Re: How do I record in what order a sort was preformed
by trs80 (Priest) on Jan 18, 2002 at 02:52 UTC
    Great code! I did notice however that it doesn't allow for
    duplicate numbers :^(
    So I revised it ever so slightly to be:
    my $count = 0; my %hash = map {$count++, $_} @array; my @sort = sort { int($a) <=> int($b) } @array; my $flag = 0; for (keys %hash) { print ', ' if $flag++; print qq|"$hash{$_}" came from | . '$array[' . $_ . ']'; }