我如何在perl上对键上的哈希数组进行排序?

问题描述 投票:0回答:1

我有一个日历,上面充斥着每天的上班时间。我喜欢每天整理小时数。.我已经计算了小时数。

loop....
    # $daguren  1..31
    # uren = sum of all the daily hours
    $daguren{$dag} += $workedhours;
...loop

# i like to sort it on "daguren" that is a number 1 .. 31   
while (($dag,$uren)=each %daguren){
    print "dag=$dag uren=$uren<br>\n";
}
perl hash
1个回答
0
投票

您要遍历键

# This sorts by working hours
my @sorted_keys = sort { $daguren{$a} <=> $daguren{$b} } keys %daguren;

# This sorts by date
my @sorted_keys = sort { $a <=> $b } keys %daguren;

foreach my $dag ( @sorted_keys )
{
     my $uren = $daguren{$dag};
     print "dag=$dag uren=$uren<br>\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.