简体   繁体   中英

How do i sort in perl my hash array on the key?

I have a calendar filled with daily hours from persons. I like to sort my hours per day.. i already counted the hours.

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";
}

You want to iterate over the keys

# 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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM