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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!sybus.sybus.com!myrddin!pdn!bcutter
  3. From: bcutter@pdnis.paradyne.com (Brooks Cutter)
  4. Subject: Re: Ordering indices based on their values
  5. Message-ID: <1992Aug27.223142.23820@pdnis.paradyne.com>
  6. Organization: /etc/organization
  7. References: <GLENN.92Aug26122420@capella.clsi.COM>
  8. Distribution: comp.lang.perl
  9. Date: Thu, 27 Aug 92 22:31:42 GMT
  10. Lines: 52
  11.  
  12. In article <GLENN.92Aug26122420@capella.clsi.COM> glenn@clsi.COM (Glenn Boysko) writes:
  13. >I was wondering if anyone knew of an efficient, simple way to produce an
  14. >ordered array of indices whose order was based on their values.  
  15.  
  16. Well the way I chose wasn't particularly efficient, but it got the
  17. job done and that's all I was after (or at least the first thing...)
  18.  
  19. I'm sure someone will point out that I'm doing it the hard way, but...
  20.  
  21. Basically this sorts the keys and goes through the list making
  22. a second associative array with the data seperated by commas...
  23.  
  24. Then I go through the sorted list (which is sorted by the time the
  25. call was open -- in reverse order in this example) and do the
  26. necessary processing..
  27.  
  28.  
  29. foreach $key (sort bynumber keys(%recdat'total_mins_open)) {
  30.     $total_mins = $recdat'total_mins_open{$key};
  31.  
  32.     if ($report'time{$total_mins} eq '') {
  33. #            print STDERR "Saving $key in report_time{$total_mins}\n";
  34.             $report'time{$total_mins} = $key;
  35.     } else {
  36. #            print STDERR "Appending $key to report_time{$total_mins}\n";
  37.             $report'time{$total_mins} .=  ",$key";
  38.     }
  39. }
  40.  
  41.  
  42. foreach $keys (reverse sort bynumber keys(%report'time)) {
  43.     @keys2 = split(',', $report'time{$keys});
  44.     foreach $keys_ctr (0..$#keys2) {
  45.         $key = $keys2[$keys_ctr];
  46.  
  47.         ...code to access values by $key...
  48.     }
  49. }
  50.  
  51. and bynumber from the perl book...
  52.  
  53. sub bynumber { $a <=> $b; }
  54.  
  55.  
  56. If anyone out there knows a better way, please speak up!
  57.  
  58.  
  59. -- 
  60. Brooks Cutter    (I: bcutter@pdnis.paradyne.com)
  61.                  (U: ...uunet!pdn!pdnis!bcutter)
  62. Unix System Administrator, Information Services
  63.         AT&T Paradyne, Largo Florida
  64.