PetaMem has asked for the wisdom of the Perl Monks concerning the following question:
consider a given, very numeric, hash:
Now - no problem to sort this hash by values and obtain a list containing the new ordering of the keys (in the example above, that'd be: 3,4,0,2,1 if ascending sort)my %hash = ( 0 => 3, 1 => 7, 2 => 5, 3 => 0, 4 => 2, );
Now such a hash is pretty trivial and also space consuming. I'd like to have the values of the hash in some array, because there are only numerical indices, no index spaces etc. So given an array:
how do I get from this the array @idx = qw(3,4,0,2,1)? I'd like to avoid to go via a hash. Thanks for any enlightment.my @vals = qw(3 7 5 0 2);
Bye
PetaMem All Perl: MT, NLP, NLU
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting by Array values, obtaining indices
by BrowserUk (Patriarch) on Jun 27, 2005 at 16:18 UTC | |
|
Re: Sorting by Array values, obtaining indices
by Limbic~Region (Chancellor) on Jun 27, 2005 at 16:19 UTC | |
by PetaMem (Priest) on Jun 27, 2005 at 16:36 UTC | |
by waswas-fng (Curate) on Jun 27, 2005 at 17:08 UTC | |
by Limbic~Region (Chancellor) on Jun 27, 2005 at 16:44 UTC | |
|
Re: Sorting by Array values, obtaining indices
by salva (Canon) on Jun 27, 2005 at 19:01 UTC | |
|
Re: Sorting by Array values, obtaining indices
by tlm (Prior) on Jun 27, 2005 at 16:26 UTC | |
|
Re: Sorting by Array values, obtaining indices
by ambrus (Abbot) on Jun 27, 2005 at 16:37 UTC |