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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

752 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