perl hash 按value/按keys 排序(多种方法)


use strict;
my%hash=('a'=>2,'b'=>3,'c'=>1);
foreachmy$key(sort{$hash{$b}<=>$hash{$a}}keys%hash){
    print$key,"=>",$hash{$key},"\n";

}

perl中如何将按hash key值排序如果是按ASCII码排序,则代码如下:

foreach my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {

    my $value = $hash{$key};

# do something with ($key, $value)

}

 

如果是按数字小到大排列,则代码如下:

foreach my $key ( sort { $hash{$a} <=> $hash{$b} } keys %hash ) {

     my $value = $hash{$key};

     #do something with ($key, $value)

}

 

如果是按数字大到小排列,则代码如下:

foreach my $key ( sort { $hash{$b} <=> $hash{$a} } keys %hash ) {

     my $value = $hash{$key};

     #do something with ($key, $value)

}

 

按KEY排列

foreach my $item (sort {$a<=>$b} keys %hash){
 print "$item  == > $hash{$item}","/n";
}


/usr/bin/perl util/mkinstallvars.pl PREFIX=. BINDIR=apps APPLINKDIR=ms LIBDIR= INCLUDEDIR=include "INCLUDEDIR=./include" ENGINESDIR=engines MODULESDIR=providers "VERSION=3.4.1" "LDLIBS=-ldl -pthread " > builddata.pm DEBUG: all keys: APPLINKDIR, BINDIR, CMAKECONFIGDIR, ENGINESDIR, INCLUDEDIR, LDLIBS, LIBDIR, MODULESDIR, PKGCONFIGDIR, PREFIX, VERSION, libdir No value given for CMAKECONFIGDIR No value given for PKGCONFIGDIR No value given for libdir DEBUG: PREFIX = . => PREFIX = /mnt/openssl-3.4.1 DEBUG: libdir = . => libdir = /mnt/openssl-3.4.1 DEBUG: BINDIR = apps => BINDIR = /mnt/openssl-3.4.1/apps, BINDIR_REL_PREFIX = apps DEBUG: LIBDIR = => LIBDIR = /mnt/openssl-3.4.1, LIBDIR_REL_PREFIX = DEBUG: INCLUDEDIR = [ include, ./include ] => INCLUDEDIR = [ /mnt/openssl-3.4.1/include, /mnt/openssl-3.4.1/include ], INCLUDEDIR_REL_PREFIX = [ include, ./include ] DEBUG: APPLINKDIR = ms => APPLINKDIR = /mnt/openssl-3.4.1/ms, APPLINKDIR_REL_PREFIX = ms DEBUG: ENGINESDIR = engines => ENGINESDIR = /mnt/openssl-3.4.1/engines, ENGINESDIR_REL_LIBDIR = engines DEBUG: MODULESDIR = providers => MODULESDIR = /mnt/openssl-3.4.1/providers, MODULESDIR_REL_LIBDIR = providers DEBUG: PKGCONFIGDIR = . => PKGCONFIGDIR = /mnt/openssl-3.4.1, PKGCONFIGDIR_REL_LIBDIR = . DEBUG: CMAKECONFIGDIR = . => CMAKECONFIGDIR = /mnt/openssl-3.4.1, CMAKECONFIGDIR_REL_LIBDIR = . /usr/bin/perl "-I." "-Mconfigdata" "-Mbuilddata" "util/dofile.pl" "-oMakefile" exporters/cmake/OpenSSLConfig.cmake.in > OpenSSLConfig.cmake Can't locate Data/Dumper.pm in @INC (@INC contains: Configurations . /mnt/openssl-3.4.1/util/../Configurations /mnt/openssl-3.4.1/util/perl /mnt/openssl-3.4.1/Configurations /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 /mnt/openssl-3.4.1/external/perl/Text-Template-1.56/lib /mnt/openssl-3.4.1/util/../external/perl/Text-Template-1.56/lib) at exporters/cmake/OpenSSLConfig.cmake.in line 5. BEGIN failed--compilation aborted at exporters/cmake/OpenSSLConfig.cmake.in line 5. make: *** [OpenSSLConfig.cmake] 错误 1
最新发布
04-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值