Tuesday, 12 November 2019

perl sort keys - hash numerically?


%hashnum=(21,2,44,11,55,5);


Method 1:[ascending order]

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

    print $hashnum{$key} . "\n";

}

Method 2:[Descending order]

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

    print $hashnum{$key} . "\n";

}
kaavannan perl blogspot

Labels: , ,