I work with GMT time zone dates with the YYYY-MM-DD HH:MM:SS format often. String compare with this is fine. The key is that the leading zeroes are absolutely necessary. This is actually what a date looks like in an SQLite DB.

I see many fine posts with sorting techniques if @$data is large.

Not considered yet is what happens if more than one thing happened on the "most recent date"? In my data sets, measured to the second, I would allow for this possibility (and it very likely could indeed happen). Code below shows just one way.

#!/usr/bin/perl -w use strict; use Data::Dumper; use Data::Dump qw(dump dd); my $data = [ { 'Color' => 'green', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '08-06-2022' }, { 'Color' => 'black', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '01-05-2019' }, { 'Color' => 'reddish', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '03-21-2021' }, { 'Color' => 'blue', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '10-11-2020' }, { 'Color' => 'white', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '08-06-2022' }, { 'Color' => 'red', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '03-21-2021' }, ]; @$data = sort{my $A = $a->{Date}; my $B = $b->{Date}; $A =~ s/(\d+)-(\d+)-(\d+)/$3-$1-$2/; $B =~ s/(\d+)-(\d+)-(\d+)/$3-$1-$2/; $B cmp $A}@$data; my $most_recent_href = $data->[0]; dd $most_recent_href; #see if there are others on same date as most recent?? foreach my $href (@$data[1..@$data-1]) { if ($href->{Date} eq $most_recent_href->{Date}) { dd $href; } else {last;} } __END__ { acc => 1111, Color => "green", Date => "08-06-2022", Step => "Platfo +rm" } { acc => 1111, Color => "white", Date => "08-06-2022", Step => "Platfo +rm" }

In reply to Re: Get most recent data based on a date from an array of hashes. by Marshall
in thread Get most recent data based on a date from an array of hashes. by Anonymous Monk

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.