home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!gossip.pyramid.com!decwrl!deccrl!news.crl.dec.com!rdg.dec.com!decvax.dec.com!genrad!jpn
- From: jpn@genrad.com (John P. Nelson)
- Subject: Re: Ordering indices based on their values
- Message-ID: <1992Aug27.151949.10917@genrad.com>
- Sender: news@genrad.com (Network News)
- Nntp-Posting-Host: maxwell
- Organization: GenRad, Inc.
- References: <GLENN.92Aug26122420@capella.clsi.COM>
- Distribution: comp.lang.perl
- Date: Thu, 27 Aug 1992 15:19:49 GMT
- Lines: 31
-
- 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. Consider the
- >following array:
-
- Try this:
-
- #!/usr/local/perl
-
- # makeindex subroutine builds and returns a sorted index array.
- # argument is a numeric array passed by reference. To create
- # an index for a string array, use "cmp" instead of "<=>"
- sub makeindex {
- local(*arr) = @_;
- local(@i) = (0 .. $#arr);
-
- sort { $arr[$b] <=> $arr[$a] } @i;
- }
-
- # test it
- @array = ( 7, 2, 4, -1, 5 );
- @index = &makeindex(*array);
-
- print "@index\n";
-
-
- john nelson
-
- uucp: mit-eddie!genrad!jpn
- domain: jpn@genrad.com
-