Can I use the same concept on a hash value?
Yes. In exactly analogous fashion. Order the keys by value and then process your values in that order:
#!/usr/local/bin/perl use strict; use Data::Dump qw[ pp ]; my %pvalues = ( 1=> 0.5453980, 2=> 0.4902384, 3=> 0.8167950, 4=> 0.2821822, 5=> 0.4693030, 6=> 0.6491767, 7=> 0.9802138, 8=> 0.1155778, 9=> 0.9585124, 10=> 0.4069490 ); my @orderedKeys = sort { $pvalues{ $b } <=> $pvalues{ $a } } keys %pvalues; my $d = my $n = values %pvalues; $pvalues{ $_ } *= $n / $d-- for @orderedKeys; pp \%pvalues; __END__ c:\test>junk68 { 1 => "0.908996666666667", 2 => "0.9804768", 3 => "1.02099375", 4 => "1.410911", 5 => "1.1732575", 6 => "0.927395285714286", 7 => "0.9802138", 8 => "1.155778", 9 => "1.06501377777778", 10 => "1.35649666666667", }
Of course, hash keys aren't intrinsically ordered, so you'll have to order them as needed for output, but one of the structure dumpers will do that for you.
In reply to Re^3: Restore the original order of an array after sort and performing some funchtion on array values
by BrowserUk
in thread Restore the original order of an array after sort and performing some funchtion on array values
by sesemin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |