Solved

sorting

Posted on 2002-03-29
5
297 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

828 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