Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: min and max in a hash of hash

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


in reply to Re^2: min and max in a hash of hash
in thread min and max in a hash of hash

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.


Comment on Re^3: min and max in a hash of hash
Download Code
Re^4: min and max in a hash of hash
by pRaNaV (Novice) on Jan 22, 2012 at 12:02 UTC

    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;
Re^4: min and max in a hash of hash
by Anonymous Monk on Jul 17, 2014 at 19:52 UTC
    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?

      Here's an example of how to accomplish this:

      use List::Util qw/min max/; my %results = map { my $year_ref = $data{$_}; my $min = min values %$year_ref; my $max = max values %$year_ref; my @min_months = grep { $year_ref->{$_} == $min } keys %$year_ref; my @max_months = grep { $year_ref->{$_} == $max } keys %$year_ref; $_ => { "min" => $min, "max" => $max, "min_months" => \@min_months, "max_months" => \@max_months } } keys %data;

      Note that this takes into account the possibility of there being several months that each have the minimum/maximum value; if your data for each year/month is guaranteed to be distinct, you could drop that and replace \@min_months with $min_months[0] and \@max_months with $max_months[0], respectively.

      There may well be a more concise way of doing this, too, but neither List::Util nor List::MoreUtils appears to contain versions of min and max that operate on key/value pairs (pairmin and pairmax? Now there'd be a worthwhile addition to these modules.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-12-29 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls