Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sorting Hashes of hashes

by remluvr (Sexton)
on Mar 06, 2012 at 15:26 UTC ( #958094=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone.
My input data are like the following:

$VAR1 = { 'dove-n' => { 'mero' => { 'rump-n' => '0.0986331918077933', 'plume-n' => '0.164895598193776', 'beak-n' => '0.148781820621755', 'head-n' => '0.541972342596793', }, 'random-v' => { 'pile-v' => '0.006058551416765 +5', 'kiss-v' => '0.005277682171536 +47', }, 'cockroach-n' => { 'mero' => { 'exoskeleton-n' => '0.0665736 +517016939', 'leg-n' => '0.440277128001941 +', }, 'random-v' => { 'suggest-v' => '0.0148588 +863336517', 'guarantee-v' => '0.00591 +007188858908', 'land-v' => '0.0149882471 +117093', },

For each keys, I need the entries to be sorted by their score, so for example I'll have something like:

dove-n mero head-n 0.541972342596793 dove-n mero beak-n 0.148781820621755 dove-n mero plume-n 0.164895598193776 dove-n mero rump-n 0.0986331918077933 dove-n random-v pile-v 0.0060585514167655 dove-n random-v kiss-v 0.00527768217153647 cockroach-n mero leg-n 0.440277128001941 cockroach-n random-v suggest-v 0.0665736517016939 cockroach-n mero exoskeleton-n 0.0148588863336517 cockroach-n random-v land-v 0.0149882471117093 cockroach-n random-v guarantee-v 0.00591007188858908

I've tried reading this: http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-an-array-by-%28anything%29? but I really don't get how I can achieve what I need. Any suggestions?
Thanks,
Giulia

Replies are listed 'Best First'.
Re: Sorting Hashes of hashes
by kcott (Archbishop) on Mar 06, 2012 at 16:28 UTC

    Your input data ($VAR1) is an invalid hashref and your example output is not sorted.

    After fixing the input and guessing you want highest score first, this code:

    #!/usr/bin/env perl use strict; use warnings; my $data = { 'dove-n' => { 'mero' => { 'rump-n' => '0.0986331918077933', 'plume-n' => '0.164895598193776', 'beak-n' => '0.148781820621755', 'head-n' => '0.541972342596793', }, 'random-v' => { 'pile-v' => '0.0060585514167655', 'kiss-v' => '0.00527768217153647', }, }, 'cockroach-n' => { 'mero' => { 'exoskeleton-n' => '0.0665736517016939', 'leg-n' => '0.440277128001941', }, 'random-v' => { 'suggest-v' => '0.0148588863336517', 'guarantee-v' => '0.00591007188858908', 'land-v' => '0.0149882471117093', }, }, }; for my $animal (keys %$data) { my @record = (); for my $type (keys %{$data->{$animal}}) { for my $part (keys %{$data->{$animal}{$type}}) { push @record, [$animal, $type, $part, $data->{$animal}{$ty +pe}{$part}]; } } map { print join(q{ } => @$_), qq{\n} } sort { $b->[3] <=> $a->[3] + } @record; }

    produces this output:

    dove-n mero head-n 0.541972342596793 dove-n mero plume-n 0.164895598193776 dove-n mero beak-n 0.148781820621755 dove-n mero rump-n 0.0986331918077933 dove-n random-v pile-v 0.0060585514167655 dove-n random-v kiss-v 0.00527768217153647 cockroach-n mero leg-n 0.440277128001941 cockroach-n mero exoskeleton-n 0.0665736517016939 cockroach-n random-v land-v 0.0149882471117093 cockroach-n random-v suggest-v 0.0148588863336517 cockroach-n random-v guarantee-v 0.00591007188858908

    The technique I've used here is to flatten the hash data into individual records and then sort those records.

    To reverse the sort order (lowest score first), just swap $a and $b, i.e.

    ... sort { $a->[3] <=> $b->[3] } ...

    -- Ken

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2023-12-08 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (35 votes). Check out past polls.

    Notices?