Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sorting

Posted on 2002-03-29
5
Medium Priority
?
339 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

636 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