avatar
Perl: hash numerical sort# Unix - 噫吁兮,危乎高哉
f*y
1
I have a hash like this:
%h = (
org, 51,
com, 7,
gov, 55,
net, 2,
biz, 5
);
How do I print it out with its value numerically sorted? It would look like:
gov 55
org 51
com 7
biz 5
net 2
Any suggestion would be appreciated. Thanks a lot!
avatar
b*t
2
foreach $k (sort keys %hash) {
print "$k => $hash{$k}\n";
}

【在 f***y 的大作中提到】
: I have a hash like this:
: %h = (
: org, 51,
: com, 7,
: gov, 55,
: net, 2,
: biz, 5
: );
: How do I print it out with its value numerically sorted? It would look like:
: gov 55

avatar
t*t
3
peng! besides, the solution is not correct...

【在 b***t 的大作中提到】
: foreach $k (sort keys %hash) {
: print "$k => $hash{$k}\n";
: }

avatar
b*t
4
foreach $value (sort { $h{$a} <=> $h{$b} } keys %h) {
print "$value => $h{$value}\n";
}

【在 t****t 的大作中提到】
: peng! besides, the solution is not correct...
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。