home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!sybus.sybus.com!myrddin!pdn!bcutter
- From: bcutter@pdnis.paradyne.com (Brooks Cutter)
- Subject: Re: Ordering indices based on their values
- Message-ID: <1992Aug27.223142.23820@pdnis.paradyne.com>
- Organization: /etc/organization
- References: <GLENN.92Aug26122420@capella.clsi.COM>
- Distribution: comp.lang.perl
- Date: Thu, 27 Aug 92 22:31:42 GMT
- Lines: 52
-
- In article <GLENN.92Aug26122420@capella.clsi.COM> glenn@clsi.COM (Glenn Boysko) writes:
- >I was wondering if anyone knew of an efficient, simple way to produce an
- >ordered array of indices whose order was based on their values.
-
- Well the way I chose wasn't particularly efficient, but it got the
- job done and that's all I was after (or at least the first thing...)
-
- I'm sure someone will point out that I'm doing it the hard way, but...
-
- Basically this sorts the keys and goes through the list making
- a second associative array with the data seperated by commas...
-
- Then I go through the sorted list (which is sorted by the time the
- call was open -- in reverse order in this example) and do the
- necessary processing..
-
-
- foreach $key (sort bynumber keys(%recdat'total_mins_open)) {
- $total_mins = $recdat'total_mins_open{$key};
-
- if ($report'time{$total_mins} eq '') {
- # print STDERR "Saving $key in report_time{$total_mins}\n";
- $report'time{$total_mins} = $key;
- } else {
- # print STDERR "Appending $key to report_time{$total_mins}\n";
- $report'time{$total_mins} .= ",$key";
- }
- }
-
-
- foreach $keys (reverse sort bynumber keys(%report'time)) {
- @keys2 = split(',', $report'time{$keys});
- foreach $keys_ctr (0..$#keys2) {
- $key = $keys2[$keys_ctr];
-
- ...code to access values by $key...
- }
- }
-
- and bynumber from the perl book...
-
- sub bynumber { $a <=> $b; }
-
-
- If anyone out there knows a better way, please speak up!
-
-
- --
- Brooks Cutter (I: bcutter@pdnis.paradyne.com)
- (U: ...uunet!pdn!pdnis!bcutter)
- Unix System Administrator, Information Services
- AT&T Paradyne, Largo Florida
-