Link to home
Start Free TrialLog in
Avatar of ytt5509Experts
ytt5509ExpertsFlag for Taiwan, Province of China

asked on

sort hash in perl

%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
Avatar of Perl_Diver
Perl_Diver

Is this some type of school assignment? Why can't you use perls sort() function?
Can we earn points just from posting links to articles? Seems like anyone could post links.
Avatar of ytt5509Experts

ASKER

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.
then maybe you want to start looking at the relational and equality operators, stuff like:

>
<
>=
<=
gt
lt
ge
le
eq
ne
cmp
<=>
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};
}

ASKER CERTIFIED SOLUTION
Avatar of jmcg
jmcg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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".
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial