home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5547 < prev    next >
Encoding:
Text File  |  1992-08-27  |  1.2 KB  |  45 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gossip.pyramid.com!decwrl!deccrl!news.crl.dec.com!rdg.dec.com!decvax.dec.com!genrad!jpn
  3. From: jpn@genrad.com (John P. Nelson)
  4. Subject: Re: Ordering indices based on their values
  5. Message-ID: <1992Aug27.151949.10917@genrad.com>
  6. Sender: news@genrad.com (Network News)
  7. Nntp-Posting-Host: maxwell
  8. Organization: GenRad, Inc.
  9. References: <GLENN.92Aug26122420@capella.clsi.COM>
  10. Distribution: comp.lang.perl
  11. Date: Thu, 27 Aug 1992 15:19:49 GMT
  12. Lines: 31
  13.  
  14. In article <GLENN.92Aug26122420@capella.clsi.COM> glenn@clsi.COM (Glenn Boysko) writes:
  15. >
  16. >I was wondering if anyone knew of an efficient, simple way to produce an
  17. >ordered array of indices whose order was based on their values.  Consider the
  18. >following array:
  19.  
  20. Try this:
  21.  
  22. #!/usr/local/perl
  23.  
  24. # makeindex subroutine builds and returns a sorted index array.
  25. # argument is a numeric array passed by reference.  To create
  26. # an index for a string array, use "cmp" instead of "<=>"
  27. sub makeindex {
  28.     local(*arr) = @_;
  29.     local(@i) = (0 .. $#arr);
  30.  
  31.     sort { $arr[$b] <=> $arr[$a] } @i;
  32. }
  33.  
  34. # test it
  35. @array = ( 7, 2, 4, -1, 5 );
  36. @index = &makeindex(*array);
  37.  
  38. print "@index\n";
  39.  
  40.  
  41.      john nelson
  42.  
  43. uucp:    mit-eddie!genrad!jpn
  44. domain:    jpn@genrad.com
  45.