Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Sorting perl hash

by paul05 (Initiate)
on Mar 06, 2021 at 17:37 UTC ( [id://11129199]=perlquestion: print w/replies, xml ) Need Help??

paul05 has asked for the wisdom of the Perl Monks concerning the following question:

I have the below hash structure, hash of hash. I need to sort the inner hash referenced by test1, test2, test3, test4. The outer keys need not be changed.
$VAR1 = { '191' => { 'test1' => { 'score' => '9.18' }, 'test2' => { 's +core' => '2.84' }, 'test3' => { 'score' => '15.62' }, 'test4' => { ' +score' => '11.84' }, '190' => { 'test1'=> { 'score' => '13.28' }, 'test2' => { 's +core' => '-47.56' }, 'test3' => { 'score' => '18.50' }, 'test4' => { ' +score' => '14.88' } } }
I am trying to sort the hash based on the 'score' value. The sorting should happen only among whats inside of the main keys '191' and '190' . See below hash structure for expected result.
$VAR1 = { '191' => {'test3' => { 'score' => '15.62' }, 'test4' => { 's +core' => '11.84' } 'test1' => { 'score' => '9.18' }, 'test2' => { 'sc +ore' => '2.84' }, ​ '190' => { 'test3' => { 'score' => '18.50' }, 'test4' +=> { 'score' => '14.88' } 'test1'=> {'score' => '13.28' }, 'test2' => { 'sco +re' => '-47.56' } } }
Sorting is to be done based on descending score value. I have tried out the below code but it kind-of sorts based on the main-key. I need output as shown in the expected hash structure.
my @sort_by_rank; for my $key1 (keys %rankBased) { for my $key2 (keys %{$rankBased{$key1}}) { @sort_by_rank = sort{ $rankBased{$b}{$key2}{score} <=> $rankBa +sed{$a}{$key2}{score} } keys %rankBased; } }
Here %rankBased is the hash. Please help.

Replies are listed 'Best First'.
Re: Sorting perl hash
by haukex (Archbishop) on Mar 06, 2021 at 17:52 UTC

    Hashes are always unordered (unless you use special modules), and so the two hashes you have posted above are identical. The most common desire is to output hashes in a sorted manner, or, in your case, you seem to want to store the keys in a sorted order in an array. However, note that the way you have posted the data above is not runnable code, and it makes it unclear what the actual hash format is - see How do I post a question effectively? and Short, Self-Contained, Correct Example. I have made a guess about the hash's format below.

    As far as I can tell, you only need to loop over the keys of the outer hash, and then sort the keys of the inner hash based on their value. That would look like this:

    use warnings; use strict; use Data::Dumper; my %rankBased = ( 190 => { test1 => { score => '13.28' }, test2 => { score => '-47.56' }, test3 => { score => '18.50' }, test4 => { score => '14.88' }, }, 191 => { test1 => { score => '9.18' }, test2 => { score => '2.84' }, test3 => { score => '15.62' }, test4 => { score => '11.84' }, }, ); for my $key1 (keys %rankBased) { my @sort_by_rank = sort { $rankBased{$key1}{$b}{score} <=> $rankBased{$key1}{$a}{score} } keys %{$rankBased{$key1}}; print Dumper(\@sort_by_rank); } __END__ $VAR1 = [ 'test3', 'test4', 'test1', 'test2' ]; $VAR1 = [ 'test3', 'test4', 'test1', 'test2' ];

    Update: See also "How do I sort a hash (optionally by value instead of key)?" in perlfaq4 and "Access and Printing of a Hash of Hashes" in perldsc.

      > However, note that the way you have posted the data above is not runnable code, and it makes it unclear what the actual hash format is -

      my guess is it's not the first time this OP is here, he just created a new account.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        my guess is it's not the first time this OP is here, he just created a new account.

        Personally I don't think it matters. The question is of good enough quality and includes a pretty much runnable code sample (it's only missing a single "}," whose location is easy enough to guess).

        Sorry, if i have not given much details. It's my first time here.
Re: Sorting perl hash
by johngg (Canon) on Mar 06, 2021 at 18:31 UTC

    Your line for my $key1 (keys %rankBased) { implies that you have an HoHoH structure to start with and, as others have mentioned, you can't have "ordered" hashes without a bit of jiggery-pokery. To retain a sorted order you need to introduce an array in there somewhere. The following code constructs an HoAoHoH structure, inserting an array between your $key1 and $key2 so that the sort is preserved.

    use strict; use warnings; use Data::Dumper; my %rankBased = ( q{191} => { test1 => { score => 9.18 }, test2 => { score => 2.84 }, test3 => { score => 15.62 }, test4 => { score => 11.84 }, }, q{190} => { test1 => { score => 13.28 }, test2 => { score => -47.56 }, test3 => { score => 18.50 }, test4 => { score => 14.88 }, }, ); my %sortedByRank; foreach my $key ( keys %rankBased ) { $sortedByRank{ $key } = [ map { { $_ => { score => $rankBased{ $key }->{ $_ }->{ score } + } } } sort { $rankBased{ $key }->{ $b }->{ score } <=> $rankBased{ $key }->{ $a }->{ score } } keys %{ $rankBased{ $key } } ]; } print Data::Dumper->Dumpxs( [ \ %sortedByRank ], [ qw{ *sortedByRank } + ] );

    The Data::Dumper output.

    %sortedByRank = ( '191' => [ { 'test3' => { 'score' => '15.62' } }, { 'test4' => { 'score' => '11.84' } }, { 'test1' => { 'score' => '9.18' } }, { 'test2' => { 'score' => '2.84' } } ], '190' => [ { 'test3' => { 'score' => '18.5' } }, { 'test4' => { 'score' => '14.88' } }, { 'test1' => { 'score' => '13.28' } }, { 'test2' => { 'score' => '-47.56' } } ] );

    I hope this guess at your data and intentions is helpful.

    Update: Changing the map line to

    map { { $_ => { %{ $rankBased{ $key }->{ $_ } } } } }

    will work if the innermost hashes have multiple key/value pairs.

    Cheers,

    JohnGG

Re: Sorting perl hash
by LanX (Saint) on Mar 06, 2021 at 17:52 UTC
    > See below hash structure for expected result.

    hashes have no order, you can't sort them

    > I need to sort the inner hash referenced by test1, test2, test3, test4.

    > ...

    > sort{ } keys %rankBased;

    Looks like you take $a and $b from the outer hash-keys not the inner.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11129199]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found