August 1999 Return to the Perl Hardware Store Slide #5

The Indirect Indirect Sort

        @fruit   = qw(Durian Cherry Apple Eggplant Banana);
        @indices = sort {$fruit[$a]   cmp $fruit[$b]  } (0 .. $#fruit);
        @ranks   = sort {$indices[$a] <=> $indices[$b]} (0 .. $#fruit);
        @ordinal = qw(first second third fourth fifth);
        for ($i=0; $i<@fruit; $i++) {
          print "The $fruit[$i] will come $ordinal[$ranks[$i]]\n";
        }
        # ``The Durian will come fourth.''
        # ``The Cherry will come third.''


Next Copyright © 1999 M-J. Dominus