How to sort perl hash on values and order the keys correspondingly (in two arrays maybe)?

前端 未结 4 1635
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 15:03

In Perl, I want to sort the keys of a hash by value, numerically:

{
  five => 5
  ten => 10
  one => 1
  four => 4
}

producing

4条回答
  •  爱一瞬间的悲伤
    2020-12-08 15:14

    my ( @nums, @words );
    do { push @nums,  shift @$_; 
         push @words, shift @$_; 
       }
        foreach sort { $a->[0] <=> $b->[0] } 
                map  { [ $h->{ $_ }, $_ ] } keys %$h
       ;
    

提交回复
热议问题