Do you know where your variables are? | |
PerlMonks |
paul05 has asked for the wisdom of the Perl Monks concerning the following question:
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' => { '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' } } }
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.$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' } } }
Here %rankBased is the hash. Please help.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; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Sorting perl hash
by haukex (Archbishop) on Mar 06, 2021 at 17:52 UTC | |
by LanX (Saint) on Mar 06, 2021 at 17:55 UTC | |
by haukex (Archbishop) on Mar 06, 2021 at 18:03 UTC | |
by paul05 (Initiate) on Mar 06, 2021 at 18:05 UTC | |
Re: Sorting perl hash
by johngg (Canon) on Mar 06, 2021 at 18:31 UTC | |
Re: Sorting perl hash
by LanX (Saint) on Mar 06, 2021 at 17:52 UTC |