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

Always a million ways to skin a cat (esp. in this case), and here is another...


#initialize array @array = (12,7,4); $i=0; foreach (@array) { #Throw in a hash to look up in a second $hash{$_} = $i; $i++; } #sort it, loop through, and look up from hash from above foreach (@num_sorted_array = sort { $a <=> $b } @array) { print "$_ came from \$array\[$hash{$_}\]\n"; }



Probably not the most efficient way, but simple enough.

Regards,
nandeya

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 04:06 UTC
    This doesn't allow for duplicate values in the array, the
    hash value will get changed to the highest number matched.
    See my earlier post for a version that allows for duplicates.
    I am also not sure if the fact duplicates aren't handled are
    a concern to the original poster, I just know I have been
    bitten by this before. :^)
    Sorry I didn't take the time to see if your code be modified
    to allow for dups.