sorting - How to sort perl hash on values and order the keys correspondingly (in two arrays maybe)? -
in perl, want sort keys of hash value, numerically:
{ 5 => 5 ten => 10 1 => 1 4 => 4 } producing 2 arrays:
(1,4,5,10) , (one, four, five, ten) and want normalize values array such numbers sequential:
(1,2,3,4) how do this?
first sort keys associated value. values (e.g. using hash slice).
my @keys = sort { $h{$a} <=> $h{$b} } keys(%h); @vals = @h{@keys}; or if have hash reference.
my @keys = sort { $h->{$a} <=> $h->{$b} } keys(%$h); @vals = @{$h}{@keys};
Comments
Post a Comment