Solved

sorting

Posted on 2002-03-29
5
284 Views
Last Modified: 2010-03-05
How to write a perl program to sort the following hash.

$a{'x1'}{'d'}=5;
$a{'x1'}{'a'}=2;
$a{'x1'}{'g'}=1;
$a{'x2'}{'x'}=5;
$a{'x2'}{'g'}=2;
$a{'x3'}{'r'}=13;
$a{'x3'}{'c'}=3;
$a{'x3'}{'b'}=4;
$a{'x3'}{'y'}=18;

after sorting, it become
$a{'x1'}{'g'}=1;
$a{'x1'}{'a'}=2;
$a{'x1'}{'d'}=5;
$a{'x2'}{'g'}=2;
$a{'x2'}{'x'}=5;
$a{'x3'}{'c'}=3;
$a{'x3'}{'b'}=4;
$a{'x3'}{'r'}=13;
$a{'x3'}{'y'}=18;

0
Comment
Question by:godman_polly
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Sapa
ID: 6904774
You cannot sort a hash due to hash is unordered data structure. Every hash value associated with key but not
with position. Although you can _output_ hash elements in sorted order by converting hash to list of hash keys,
and sorting this list:

my %a;

$a{'x1'}{'d'}=5;
$a{'x1'}{'a'}=2;
$a{'x1'}{'g'}=1;
$a{'x2'}{'x'}=5;
$a{'x2'}{'g'}=2;
$a{'x3'}{'r'}=13;
$a{'x3'}{'c'}=3;
$a{'x3'}{'b'}=4;
$a{'x3'}{'y'}=18;

# $okey iterates thru all 'outer' hash keys ordered
# by their names
foreach my $okey (sort keys %a) {
    # $inner is a reference to anonymous hash
    # (2nd level of your data structure)    
    my $inner = $a{$okey};

    # @sorted contains 'inner' keys ordered by values
    my @sorted = sort {$inner->{$a} <=> $inner->{$b}} keys %$inner;

    # print in sorted order
    foreach my $ikey (@sorted) {
        printf "\$a{'$okey'}{'$ikey'}=$inner->{$ikey};\n";
    }
}


Andrey
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6904777
What is the logic for sorting here ?
JD
0
 

Author Comment

by:godman_polly
ID: 6905047
It seems that Sapa's suggested answer doesn't work.
0
 
LVL 5

Accepted Solution

by:
Sapa earned 30 total points
ID: 6905066
godman_polly,

what do you mean? The script I have written works and produces follow output:

$a{'x1'}{'g'}=1;
$a{'x1'}{'a'}=2;
$a{'x1'}{'d'}=5;
$a{'x2'}{'g'}=2;
$a{'x2'}{'x'}=5;
$a{'x3'}{'c'}=3;
$a{'x3'}{'b'}=4;
$a{'x3'}{'r'}=13;
$a{'x3'}{'y'}=18;

what's exactly the same as you wrote. Note, I said it's produces ORDERED OUTPUT, not makes hash ordered. No order is a hash's principial property.

If you want the data structure which has both, an order (like  arrays) and key-value associations (like hashes), you may want to look Tie::IxHash.
(http://www.cpan.org/authors/id/GSAR/Tie-IxHash-1.21.tar.gz)

If you want to get absolutely strict answer on your question - get it:

Perl hashes cannot be sorted due to they have no order property by nature.

Please clarify, what did you mean writing "doesn't work".

Andrey

0
 

Author Comment

by:godman_polly
ID: 6906470
I can run the script now. Your ans is excellent.
But I would like to know what kind of structure of
$inner->{$a} is?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now