codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  sort hash by value, numerically  mimir at 13:48 on Tuesday, March 04, 2008
 

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


  Re: sort hash by value, numerically  amitbhosale at 07:00 on Monday, March 10, 2008
 

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


  Re: sort hash by value, numerically  mimir at 08:20 on Monday, March 10, 2008
 

Thanks a lot.










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
• mkdirs
• Re: Web Development Project
• ** Site Hacked ** javascript inserted...
• Losing background/text color when converting HTML to PDF
• hidden div block when displayed, displays at an offset of 200px only in IE 6..
• help - sketch
• Re: Perl Script - File Handling.
• Open a file from website
• Re: to open 5 terminals from one and also execute different commands on each terminal


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008