home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!clsi!glenn
- From: glenn@clsi.COM (Glenn Boysko)
- Subject: Ordering indices based on their values
- Message-ID: <GLENN.92Aug26122420@capella.clsi.COM>
- Sender: usenet@clsi.COM
- Organization: CAD Language Systems Inc.
- Distribution: comp.lang.perl
- Date: 26 Aug 92 12:24:20
- Lines: 48
-
-
- Hello Perl Hackers,
-
- 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. Consider the
- following array:
-
- Index | Value
- ------+------
- 0 | 7
- 1 | 2
- 2 | 4
- 3 | -1
- 4 | 5
-
- I would like a new array whose values are indices of the input array. The
- values of the new array are ordered such that their values in the original
- array are ordered in descending order. The resulting array for our example
- is:
-
- Index | Value
- ------+------
- 0 | 0
- 1 | 4
- 2 | 2
- 3 | 1
- 4 | 3
-
- Note that an appropriate solution would be to use an associative array to
- store (value, index) entries and then sort the keys. However, this will not
- work if the values of the original array are not distinct.
-
- Once this array has been generated, I can process each entry in the orignal
- array according to the descending order of the values.
-
- Any Ideas?
- Glenn
-
-
-
- --
- ......................... * *
- ..=====.==....=====..===. | Glenn Boysko | "An eye for an eye,
- .======.==...===.....===. | CAD Language Systems, Inc | a tooth for a tooth,
- .==.....==....===....===. | | vote for me and
- .======.====....===..===. | glenn@clsi.COM OR | I'll set you free."
- ..=====.====.=====...===. | uunet!clsi!glenn | -- Daniel Ash
- ......................... * *
-