in reply to Sorting Arrays

my @indicies = sort { $a[$a] <=> $a[$b] } 0..$#a;

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Sorting Arrays
by gri6507 (Deacon) on Jan 08, 2002 at 21:49 UTC
    thank you very much. But why does this work??
      my @indicies = sort { $a[$a] <=> $a[$b] } 0..$#a;

      merlyn is sorting a list of integers (0..$#a) corresponding to the indices of the elements of your array @a. Recall that within sort code blocks, $a and $b are special variables representing the two variables to be ordered each time the block is evaluated. So here,  $a[$a] <=> $a[$b] means sort the indices of the array by the contents of its elements.

      It's a bit confusing in this case because of the use of @a as your array variable and the use of $a and $b as special variables to sort.

      HTH,

      Josh