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

Re^3: Sorting a hash of hashes

by NetWallah (Canon)
on May 15, 2012 at 13:48 UTC ( [id://970639]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Sorting a hash of hashes
in thread Sorting a hash of hashes

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

Replies are listed 'Best First'.
Re^4: Sorting a hash of hashes
by mrc (Sexton) on May 15, 2012 at 18:50 UTC
    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.
      Since you did not specify it, I assume you want the comparison done on "value1" only. Based on that, here is the code:
      my %rehash; for my $SUBNAME (sort keys %HoH){ for my $testname (sort keys %{ $HoH{$SUBNAME} }) { my $val = $HoH{$SUBNAME}{$testname}{value1}; if (exists $rehash{$testname}{$SUBNAME}{value1} and $rehash{$testname}{$SUBNAME}{value1} > $val){ # Do not update - greater value exists }else{ $rehash{$testname}{$SUBNAME} = $HoH{$SUBNAME}{$testname}; } } } for my $testname (sort keys %rehash){ print "For $testname:\n"; for my $SUBNAME(sort keys %{ $rehash{$testname} }){ print " $SUBNAME has " , map( {" $_=$rehash{$testname}{$SUBNAME}{$_};"} sort keys %{$r +ehash{$testname}{$SUBNAME}}), ,"\n"; } } __OUTPUT__ For Test1: SUB1 has value1=2300; value2=0.01; SUB2 has value1=2800; value2=0.05; SUB3 has value1=2700; value2=0.25; For Test2: SUB1 has value1=5000; value2=0.34; SUB2 has value1=5500; value2=0.34; SUB3 has value1=5800; value2=0.45; For Test3: SUB1 has value1=3000; value2=0.10; For Test4: SUB1 has value1=7000; value2=0.33;
      At this point, your original problem has been long solved, so please make further enhancements yourself.

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

        Now the code is more readable. Thank you!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-26 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found