Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Sorting Hashes

Posted on 2005-03-29
Medium Priority
Last Modified: 2012-08-13
I have a set of hashes, the hash key $val is used as a primary key in each hash.
Each hash presents a simple counter, counting up the amount of states for
the hash key. Enum this hashes look like eg:

for my $val (keys %ConTotal ){
 print "Adress: $val, Total: $ConTotal{$val}, FIN: $ConStateFin{$val}, SYN: $ConStateSyn{$val}\n";

Now i would like to display all hashes in a table and sort descending for eg. %ConStateFin,
what is the best way of doing this ?
Many Thanks
Question by:MKrauss
  • 2
  • 2
LVL 18

Accepted Solution

kandura earned 1000 total points
ID: 13655673
you can sort the keys of %ConTotal according to whatever criteria you want:

    for my $val( sort { $ConStateFin{$b} <=> %ConStateFin{$a} } keys %ConTotal ) {

This would sort descending on the values of %ConStateFin. If you switch $a and $b around, it would sort ascending. The comparison is <=>, which is numerical. If the values of %ConStateFin are strings, you might want to use cmp instead:

    for my $val( sort { $ConStateFin{$a} cmp %ConStateFin{$b} } keys %ConTotal ) {

Author Comment

ID: 13655774
Great ! Many Thanks
LVL 18

Expert Comment

ID: 13655885
I hope you caught my typo in the examples: the second mention of ConStateFin should be preceded by a $, not a %:

    for my $val( sort { $ConStateFin{$b} <=> $ConStateFin{$a} } keys %ConTotal ) {


Author Comment

ID: 13656057
yes i noticed the typo, thought you're testing me .... thanks
LVL 16

Expert Comment

ID: 13660077
What happens to the sort when a key in %Contotal is not present as a valid key of %ConStateFin

for my $val( sort { $ConTotal{$a} cmp %ConTotal{$b} } keys %ConTotal ) {

Also, are you expecting that each key will either be present in all the hashes or in none of them?? What happens if a key in
%ContStateFin is not present in %Contotal...??


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question