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.)


In reply to Re^5: min and max in a hash of hash by AppleFritter
in thread min and max in a hash of hash by nagalenoj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.