|
#the following code almost sorts the has by value.
#how can i completely sort the has by value?
%ages = (hmm=>1, siggs=>2, john=>23, anthony=>99, olav=>22, lila=>17);
foreach $key (sort {$ages {$a} cmp $ages {$b}} keys %ages )
{
print STDOUT "$ages{$key}\n";
}
|
|
|
Hi,
you can use the number comparison operator <=>.
With the form op1 <=> op2 this operator returns 1 if op1 is greater than op2, 0 if op1 equals op2, and -1 if op1 is less than op2.
#!/usr/bin/perl -w
%ages = (hmm=>1, siggs=>2, john=>23, anthony=>99, olav=>22, lila=>17);
foreach $key (sort { $ages {$a} <=> $ages {$b}} keys %ages )
{
print STDOUT "$ages{$key}\n";
}
========o/p==========
$ perl test.pl
1
2
17
22
23
99
|
|
|
|
|
Nice post about numerically. I like your blog very much because it has very interesting articles of different topics like 70-293 braindumps, 70-270 dumps and ccna exam dumps and their tips and tricks. I am a very big googler and search on different topics. Between searching i found your nice blog. Thanks for your this great blog.
|
|
|
|
|
|
|
|