Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to get the Max values' key!

by Anno (Deacon)
on Aug 13, 2007 at 10:29 UTC ( [id://632181]=note: print w/replies, xml ) Need Help??


in reply to How to get the Max values' key!

Finish it? You haven't offered any beginning to finish.

Here is one way:

use List::Util qw( max); while ( my ( $first, $h) = each %r ) { my $second = { reverse %$h }->{ max values %$h}; print "\%r{$first}{$second} ==> $h->{ $second}\n"; }
The code assumes that none of the second level hashes is empty, so the maximum always exists. If the maximum isn't unique (say if $r{123}{c} = 3 had also been given), a random key with the maximum value is selected.

The solution is inefficient if the second level hashes are big because for each of them the full inverted hash is built just to pick out a single value.

Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-18 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found