Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: min and max in a hash of hash

by ikegami (Pope)
on Jul 12, 2010 at 06:04 UTC ( #848929=note: print w/replies, xml ) Need Help??


in reply to min and max in a hash of hash

use List::Util qw( min max ); my $max_y = max keys %Year; my $max_m = max keys %{ $Year{$max_y} }; my $min_y = min keys %Year; my $min_m = min keys %{ $Year{$min_y} };

Replies are listed 'Best First'.
Re^2: min and max in a hash of hash
by wwe (Friar) on Jul 12, 2010 at 16:30 UTC
    Hi ikegami, could you enlighten me what is wrong on using List::Util? the following code works fine, after I corrected the hash provided:
    foreach my $y (sort keys %Year) { my $min_y = List::Util::min values %{ $Year{$y} }; print "min of [$y] is [$min_y]\n"; }
    Edit: this works for sure only if you doesn't care about the month of the min/max value.

      That will give you the minimum value for each year. The OP wanted just the overall minimum. If he doesn't need the y/m of the minimum, then he could use

      use List::Util qw( min max ); my $min = min map { min values %{$Year{$_}} } keys %Year; my $max = max map { max values %{$Year{$_}} } keys %Year;

      Update: Fixed s/values/keys/ as per reply.

        hi, slight correction

        use List::Util qw( min max ); my $min = min map { min values %{$Year{$_}} } keys %Year; my $max = max map { max values %{$Year{$_}} } keys %Year;
        I believe he was interested in both the year and month as am I. I currently have the same issue. I have the same data structure using a hash of hash. I would like to report for each year, the month and value of the min and max value. How can your existing example be modified to accommodate this?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://848929]
help
Chatterbox?
[Lady_Aleena]: perl -e 'my $var = "1.0"; print $var;' if it is in a variable also returns 1.0, though perl -e 'my $var = 1.0; print $var;' returns 1.
[1nickt]: In my case I can simply pass sprintf '%.1f', 1.0 (to Types::Standard:: Int), but what if you didn;t know the precision of the number you were working with? Seems I must be missing something. Oh well, my test list is complete, mooving on ...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2017-05-24 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?