?
Solved

sort hash in perl

Posted on 2006-05-21
14
Medium Priority
?
5,022 Views
Last Modified: 2008-03-06
%aList = ('car'=>300,'train'=>150,'truck'=>100,'bike'=>330);

I need to write a program to sort this by the order of key and value respectively without
using the sort functions provided by the language
0
Comment
Question by:ytt5509Experts
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16730478
Is this some type of school assignment? Why can't you use perls sort() function?
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16730753
Can we earn points just from posting links to articles? Seems like anyone could post links.
0
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.

 

Author Comment

by:ytt5509Experts
ID: 16730773
I'm preparing a sets of questions and answers for my interview, not school work.
And, I really need a workable version of functions, not links, Thanks.
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16731561
then maybe you want to start looking at the relational and equality operators, stuff like:

>
<
>=
<=
gt
lt
ge
le
eq
ne
cmp
<=>
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 16733524
Why can you not use sort?  Are you saying that you can't do anything like:

#!/usr/bin/perl -w
use strict;

    my %aList = ('car'=>300,'train'=>150,'truck'=>100,'bike'=>330);
   
    print "Sorted by key, alphabetically:\n\n";
    foreach my $key (sort keys(%aList)) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by key, reverse alphabetically:\n\n";
    foreach my $key (reverse (sort keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by value, lowest to highest:\n\n";
    foreach my $key (sort value_L2H (keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by value, highest to lowest:\n\n";
    foreach my $key (sort value_H2L (keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }

sub value_L2H {
   $aList{$a} <=> $aList{$b};
}

sub value_H2L {
   $aList{$b} <=> $aList{$a};
}

0
 
LVL 20

Accepted Solution

by:
jmcg earned 672 total points
ID: 16734430
If you were interviewing with me, you'd be in trouble until you explained that you didn't really mean "sort the hash". The elements of the hash can be retrieved in a specific order, most commonly by sorting the keys. And there are modules that support expanded hash objects with additional properties, such as maintaining the insertion order.
0
 

Author Comment

by:ytt5509Experts
ID: 16735314
this is the question that made up by the supervisor, and our job is to turn in
the answer, and myself is not a decent perl programmer. What I mean for
the problem to solve is:

is it possible to print out a hash contents in ASCII order with respect to the
key(car, bike, truck) and the value(330, 300, 150), without using the "sort" function?
The result will be the same like mjcoyne has provided, but without using "sort".
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 664 total points
ID: 16736698
You can
use Tie::IxHash;
to keep hashes sorted
0
 
LVL 20

Expert Comment

by:jmcg
ID: 16737812
So is it a question of using some clever thing, like the fancier hash Ozo has pointed to (with the SortByKey and SortByValue re-ordering methods -- what do you want to bet that they're implemented using 'sort'?) or of implementing your own sort function in Perl?
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 664 total points
ID: 16741566
My answer to such a question would be:  "Yes, a function can be written in Perl (or as a C extension) that will sort a hash any way you like without relying on Perl's internal sort function.  However, since I'll be working for you and you'll be paying me by the hour, I would never waste time on such a pursuit, opting instead to rely on Perl's well-known and clearly predictable internal function.  These types of decisions would allow me to work more efficiently by remaining focused on the purpose of the code I'm writing, rather than meandering into code backwaters which might actually introduce subtle and hard-to-track bugs into the production code".

:)

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

850 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