我有这个结构:
'$self' => {
'stepTimePercentage' =>{
'id12' => {
'percentage' => '1.00'
},
'id15' => {
'percentage' => '30.00'
},
'id4' => {
'percentage' => '20.00'
},
'id9' => {
'percentage' => '15.00'
},
}
}
我想按照'百分比'的值对此结构进行排序。我尝试了以下内容,但我得到了:"在数字比较中使用未初始化的值(< =>)"。
foreach my $key (sort{ $self->{stepTimePercentage}->{percentage}{$b} <=> $self->{stepTimePercentage}->{percentage}{$a} } keys %{$self->{stepTimePercentage}}) {
print "$key - $self->{stepTimePercentage}->{$key}->{percentage} % \n";
}
然后我尝试了这个(我得到了&#34;全局符号&#34; $ key&#34;需要明确的包名&#34;):
foreach my $key (sort{ $self->{stepTimePercentage}{key}{$b} <=> $self->{stepTimePercentage}{$key}{$a}} keys %{$self->{stepTimePercentage}}) {
print ("$key - $self->{stepTimePercentage}->{$key}->{percentage} % \n");
}
答案 0 :(得分:4)
你几乎就在那里。你要排序的关键是三级哈希的第二级,所以你想要:
foreach my $key (sort {
$self->{stepTimePercentage}{$b}{percentage}
<=>
$self->{stepTimePercentage}{$a}{percentage}
} keys %{$self->{stepTimePercentage}}) {
print "$key - $self->{stepTimePercentage}->{$key}->{percentage} % \n";
}