Good afternoon!

I'm a researcher in the Humanities and I'm trying my hand at a bit of programming to aid in the analysis of a large dataset. I've got as far as storing my data in a hash of hashes. They keys are the pseudonyms of my participants and the values are the dates (in YYYYMM format) that they attended my programme, and then I also have how many times they attended each month (but this isn't relevant to my question today). What I want to do is, for each participant, print out the earliest and latest date that they attended.

I found some code on StackExchange (reproduced below) which worked okay for the example given on that site, but I think I have misunderstood something because it doesn't produce the expected output using an extract from my dataset. Included below is the code I'm using, the actual output, and the expect output. I would be very grateful for any pointers about what I've done wrong here. Thanks very much in advance.

my %counts = ( "Adam" => { "201708" => 1, "201703" => 1, "201804" => 1, " +201603" => 1, "201705" => 1, "201702" => 1, "201608" => 1, "201704" = +> 1,}, "Sam" => { "201803" => 1, "201801" => 1 }, ); use List::UtilsBy 'max_by'; foreach my $key ( keys %counts ) { my $subhash = $counts{$key}; my $maximal = max_by { $subhash->{$_} } keys %$subhash; print "$key, $maximal\n"; } use List::UtilsBy 'min_by'; foreach my $key ( keys %counts ) { my $subhash = $counts{$key}; my $minimal = max_by { $subhash->{$_} } keys %$subhash; print "$key, $minimal\n"; }
------------ OUTPUT Adam, 201704 Sam, 201801 Adam, 201704 Sam, 201801 ------------- EXPECTED OUTPUT Adam, 201804 Sam, 201803 Adam, 201603 Sam, 201801

In reply to Using List::UtilsBy to print max and min values from a hash of hashes by Izzy_Murph

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.