Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How do I keep the first key in a sorted hash?

by Anonymous Monk
on Feb 17, 2014 at 11:26 UTC ( [id://1075158]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
if you have a hash with numerical values both as keys and as values,in order to keep the value of the key with the lowest value amongst the keys, would you use the following code?
for $key ( sort {$a<=>$b} keys %hash) { print "($key)->($hash{$key})\n"; }

And if so, how can I store only the key with the lowest value (and it's corresponding value of course)?

Replies are listed 'Best First'.
Re: How do I keep the first key in a sorted hash?
by Athanasius (Archbishop) on Feb 17, 2014 at 12:24 UTC

    TMTOWTDI. The core module List::Util can be useful here:

    #! perl use strict; use warnings; use List::Util qw( first min ); my %h = ( 17 => 2, 23 => 5, 42 => 1, 7 => 3, 35 => 4, ); my $min_key = min keys %h; printf "The smallest key is %d, and its associated value is %2d\n", $min_key, $h{$min_key}; my $min_val = min values %h; printf "The smallest value is %d, and its associated key is %2d\n", $min_val, first { $h{$_} == $min_val } keys %h;

    Output:

    22:21 >perl 876_SoPW.pl The smallest key is 7, and its associated value is 3 The smallest value is 1, and its associated key is 42 22:21 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How do I keep the first key in a sorted hash?
by Anonymous Monk on Feb 17, 2014 at 11:31 UTC
    Ah, nevermind, I just thought of passing the keys to an array and then sort the array:
    @sorted_positions = sort { $a <=> $b };
    Then, I get the $sorted_positions[0] element and everything is OK!

      You can even do away with the intermediate array variable:

      my $min = ( sort { $a <=> $b } keys %hash )[ 0 ];
      If your hash is large, then using the sort function just to retrieve the smallest value is inefficient overkill. Use a variable, say $min, and set it to any key that you know to exist (or to a very large value such as 1e10 that you know will larger than at least one of the keys). Then you can simply do something like this:
      my $min = 1e10; for my $key (keys %h) { $min = $key if $key < $min; } # now, $min is the smallest key. The associated value is simply $h{$mi +n}
      Now, depending on how you get the data in the first place, you could also maintain the $min variable when populating the hash, thereby avoiding to read all the keys once more.
Re: How do I keep the first key in a sorted hash?
by pajout (Curate) on Feb 17, 2014 at 12:32 UTC
    As you probably know, hashes have not any order of keys by definition, so I see the simplest way to keep minimal key value in a special scalar variable.

    But if you require 'special' hash, which naturally stores just one key/value pair, i.e. if new key/value pair is written into hash, one of them with greater key is deleted, you can implement your own hash using tie (http://perldoc.perl.org/perltie.html).

Log In?
Username:
Password:

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

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

    No recent polls found