Solved

sorting

Posted on 2002-03-29
5
255 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
Comment Utility
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
Comment Utility
What is the logic for sorting here ?
JD
0
 

Author Comment

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

Accepted Solution

by:
Sapa earned 30 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
OTRS Installation 1 276
Perl for loop for 2000 ms 7 91
Perl strange behaviour 5 63
pipe to sed or perl, please help 5 120
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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video discusses moving either the default database or any database to a new volume.

762 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

10 Experts available now in Live!

Get 1:1 Help Now