You can use Time::Piece (which is core) to get the epoch seconds from the date. The epoch key/value pair can then be added to the hash ref. for sort'ing numerically and then delete'ed from the hash ref. afterwards. You can then take the last element from the sorted items to get the latest date.
use strict;
use warnings;
use Time::Piece;
use Data::Dumper;
my $data = [
{
Color => q{green},
Step => q{Platform},
acc => q{1111},
Date => q{08-06-2022},
},
{
Color => q{black},
Step => q{Platform},
acc => q{1111},
Date => q{01-05-2019},
},
{
Color => q{blue},
Step => q{Platform},
acc => q{1111},
Date => q{10-11-2020},
},
{
Color => q{white},
Step => q{Platform},
acc => q{1111},
Date => q{01-03-2022},
},
{
Color => q{red},
Step => q{Platform},
acc => q{1111},
Date => q{03-21-2021},
},
];
my @filtered = (
map {
delete $_->{ epoch };
$_;
}
sort { $a->{ epoch } <=> $b->{ epoch } }
map {
$_->{ epoch } =
Time::Piece->strptime( $_->{ Date }, q{%m-%d-%Y} )->epo
+ch();
$_;
}
@{ $data }
)[ -1 ];
print Data::Dumper->Dumpxs( [ \ @filtered ], [ qw{ *filtered } ] );
Produces
@filtered = (
{
'Color' => 'green',
'Date' => '08-06-2022',
'Step' => 'Platform',
'acc' => '1111'
}
);
I hope this is helpful.
Update: Expanded wording re. use of the temporary epoch key/value pair.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.