Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to retrieve the worst value?

by Anonymous Monk
on Apr 01, 2004 at 07:54 UTC ( [id://341546]=perlquestion: print w/replies, xml ) Need Help??

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

I have a hash that keeps the time each process is taking in evaluating some data. My question is how can I retrieve the worst time-process?
my %hash = ( processone => 1, processtwo => 0.005, processthree => 3 ) +;
and display "the worst process is processthree with a time of 3 minutes"

Should I read the values into an array, sort them and retrieve the position?, any better idea?
Thanks in advance!!

Replies are listed 'Best First'.
Re: How to retrieve the worst value?
by BrowserUk (Patriarch) on Apr 01, 2004 at 08:07 UTC

    No need to sort or transfer values to an array

    use List::Util qw[ reduce ]; my $worstKey = reduce{ $hash{ $a } < $hash{ $b } ? $a : $b } keys %hash; print "The worst is $worstKey with a time of $hash{ $worstKey } minute +s";

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: How to retrieve the worst value?
by davido (Cardinal) on Apr 01, 2004 at 08:05 UTC
    You could do something like this:

    my $worst; my $max = 0; while ( my ( $key, $value ) = each %hash ) { if ( $value > $max ) { $worst = $key; $max = $value } } print "The worst process is $worst, with a time of $max minutes.\n";

    This snippet assumes no two processes will take exactly the same length of time. However, if there are two that take the exact amount of time, only one of them will be shown. This may or may not be important to you.


    Dave

Re: How to retrieve the worst value?
by pbeckingham (Parson) on Apr 01, 2004 at 13:52 UTC

    Turn the hash inside-out, then sort the keys in descending order and pick the first.

    my %hash = (processone => 1, processtwo => 0.005, processthree => 3) +; my %hash2 = map {$hash{$_} => $_} keys %hash; my $worst = (reverse sort keys %hash2)[0]; print "the worst process is $hash2{$worst} with a time of $worst min +utes\n";

      Why all the excess complexity? sort { $h{$a} <=> $h{$b} } should be just fine here.
      %hash = (processone => 1, processtwo => 0.005, processthree => 3); @_ = sort { $hash{$a} <=> $hash{$b} } keys %hash; print $_[0], ' => ', $hash{$_[0]}, " (lowest)\n"; print $_[-1], ' => ', $hash{$_[-1]}, " (highest)\n";

      --
      [ e d @ h a l l e y . c c ]

      Hi,

      Why create an extra hash?

      print +(sort { $a <=> $b } values %hash)[-1];
      The sort by default sorts alfanumeric, so in your case 30 is better than 4.

      ---------------------------
      Dr. Mark Ceulemans
      Senior Consultant
      BMC, Belgium

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found