perl中如何将按hash的排序方法?如果是按ASCII码排序,则代码如下:foreachmy$key(sort{$hash{$a}cmp$hash{$b}}keys%hash){my$value=$
perl中如何将按hash的排序方法?
如果是按ASCII码排序,则代码如下:foreachmy$key(sort{$hash{$a}cmp$hash{$b}}keys%hash){my$value=$hash{$key}#dosomethingwith($key,$value)}如果是按数字小到大排列,则代码如下:foreachmy$key(sort{$hash{$a}<=>$hash{$b}}keys%hash){my$value=$hash{$key}#dosomethingwith($key,$value)}如果是按数字大到小排列,则代码如下:foreachmy$key(sort{$hash{$b}<=>$hash{$a}}keys%hash){my$value=$hash{$key}#dosomethingwith($key,$value)}按KEY排列foreachmy$item(sort{$a<=>$b}keys%hash){print"$item ==>$hash{$item}","/n"}perl,一个简单的哈希?
如果我这么写:print"$count{"fred"}"(在哈希前后加上双引号)程序就会报错:Unquotedstring"fred"mayclashwithfuturereservedwordat./perl.plline762.Stringfoundwhereoperatorexpectedat./perl.plline762,near"fred"}""syntaxerrorat./perl.plline762,atEOFExecutionof./perl.plabortedduetocompilationerrors.这是为什么啊?那是因为你的“”里面还有“”,而perl会把第二个“和前面的”匹配,导致出错,你可以这样print"$count{"fred"}"perl如何把hash按照数值大小排序?
my %hash my @keys=sort { $hash{$a} <=> $hash{$b} } keys %hash 这样就可以了,直接循环用下面的。 foreach my $key ( sort { $hash{$a} <=> $hash{$b} } keys %hash) { }本文链接:http://21taiyang.com/SoccerSports/8410023.html
perl和《pinyin:hé》Python转载请注明出处来源