Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Sorting a hash of hashes

by NetWallah (Canon)
on May 15, 2012 at 05:56 UTC ( #970568=note: print w/replies, xml ) Need Help??


in reply to Sorting a hash of hashes

Considerably more compile-able than Utilitarian's code :
use strict; use warnings; my %HoH = ( SUB1 => { Test1 => { value1 => "2300", value2 => "0.01", }, Test2 => { value1 => "5000", value2 => "0.34", }, }, SUB2 => { Test1 => { value1 => "2800", value2 => "0.05", }, }, ); my %hoa; for my $I (sort keys %HoH){ for my $J (sort keys %{ $HoH{$I} }) { for my $K (sort keys %{ $HoH{$I}{$J} } ){ push @{$hoa{$J}}, [$I,$K, $HoH{$I}{$J}{$K}]; } } } for my $k (sort keys %hoa){ my $entry=$hoa{$k}; print "For $k\n"; for my $item(sort {$a->[1] cmp $b->[1] } @$entry){ print " $item->[0] has $item->[1]=$item->[2]\n" } } __OUTPUT__ For Test1 SUB1 has value1=2300 SUB2 has value1=2800 SUB1 has value2=0.01 SUB2 has value2=0.05 For Test2 SUB1 has value1=5000 SUB1 has value2=0.34
The code is not the prettiest, - but then, neither is your data structure.

             I hope life isn't a big joke, because I don't get it.
                   -SNL

Replies are listed 'Best First'.
Re^2: Sorting a hash of hashes
by mrc (Sexton) on May 15, 2012 at 06:52 UTC
    Thanks all! NetWallah, your example seems exactly what I'm looking for. Time to study it and learn some new things about sorting hashes :) Please tell me how to only display the biggest value. Output example:
    __OUTPUT__ For Test1 SUB2 has value1=2800 SUB2 has value2=0.05 For Test2 SUB1 has value1=5000 SUB1 has value2=0.34
      Ok - here you go (Improved names of variables), and did "max" as requested:
      my %rehash; for my $SUBNAME (sort keys %HoH){ for my $testname (sort keys %{ $HoH{$SUBNAME} }) { for my $value_name (sort keys %{ $HoH{$SUBNAME}{$testname} } ){ my $val = $HoH{$SUBNAME}{$testname}{$value_name}; if (exists $rehash{$testname}{$value_name} and $rehash{$testname}{$value_name}[1] > $val){ # Do not update - greater value exists }else{ $rehash{$testname}{$value_name} = [$SUBNAME, $val]; } } } } for my $testname (sort keys %rehash){ print "For $testname:\n"; for my $value_name(sort keys %{ $rehash{$testname} }){ print " $rehash{$testname}{$value_name}[0] has $value_name\=$re +hash{$testname}{$value_name}[1]\n"; } }

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

        Thanks for the update!
        I have updated the example with few more values. How to display the first 2 (or more) results not just the biggest value and how to sort by first value1 and add value2 as a pair?
        I suppose the values must be added to an array, sorted then display first, second, third etc. element of that array. Hash of hashes or hash of arrays are still unclear for me.
        __OUTPUT__ For Test1 SUB2 has value1=2800 and value2=0.05 SUB3 has value1=2700 and value2=0.25 For Test2 SUB2... SUB3... etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://970568]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2018-02-23 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (300 votes). Check out past polls.

    Notices?