?
Solved

sorting

Posted on 2002-03-29
5
Medium Priority
?
329 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
[X]
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
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 120 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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

764 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