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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!clsi!glenn
  3. From: glenn@clsi.COM (Glenn Boysko)
  4. Subject: Ordering indices based on their values
  5. Message-ID: <GLENN.92Aug26122420@capella.clsi.COM>
  6. Sender: usenet@clsi.COM
  7. Organization: CAD Language Systems Inc.
  8. Distribution: comp.lang.perl
  9. Date: 26 Aug 92 12:24:20
  10. Lines: 48
  11.  
  12.  
  13. Hello Perl Hackers,
  14.  
  15. I was wondering if anyone knew of an efficient, simple way to produce an
  16. ordered array of indices whose order was based on their values.  Consider the
  17. following array:
  18.  
  19.     Index | Value
  20.     ------+------
  21.       0   |  7
  22.       1   |  2
  23.       2   |  4
  24.       3   | -1
  25.       4   |  5
  26.  
  27. I would like a new array whose values are indices of the input array.  The
  28. values of the new array are ordered such that their values in the original
  29. array are ordered in descending order.  The resulting array for our example
  30. is:
  31.  
  32.     Index | Value
  33.     ------+------
  34.       0   |  0
  35.       1   |  4
  36.       2   |  2
  37.       3   |  1
  38.       4   |  3
  39.  
  40. Note that an appropriate solution would be to use an associative array to
  41. store (value, index) entries and then sort the keys.  However, this will not
  42. work if the values of the original array are not distinct.
  43.  
  44. Once this array has been generated, I can process each entry in the orignal
  45. array according to the descending order of the values.
  46.  
  47. Any Ideas?
  48. Glenn
  49.  
  50.  
  51.     
  52. --
  53. ......................... *                           *   
  54. ..=====.==....=====..===. | Glenn Boysko              | "An eye for an eye,
  55. .======.==...===.....===. | CAD Language Systems, Inc |  a tooth for a tooth,
  56. .==.....==....===....===. |                           |  vote for me and
  57. .======.====....===..===. | glenn@clsi.COM  OR        |  I'll set you free."
  58. ..=====.====.=====...===. | uunet!clsi!glenn          |        -- Daniel Ash
  59. ......................... *                           *
  60.