[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

perl, how to sort hash key in descending order

$hash{"v1"} = "11";
$hash{"v2"} = "22";
$hash{"v4"} = "44";
$hash{"v3"} = "33";

How do I sort the hash by the key in descending order?
0
rgbcof
Asked:
rgbcof
  • 2
1 Solution
 
ozoCommented:
reverse sort keys %hash
0
 
ozoCommented:
print "\$hash{\"$_\"} = \"$hash{$_}\"\n" for sort {$b cmp $a} keys %hash;
0
 
rgbcofAuthor Commented:
Both ways work.  Thanks Ozo.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now