in reply to Get most recent data based on a date from an array of hashes.
I removed the superfluous, identical Step and acc key/value pairs throughout. I moved the wanted element to the centre of the @$data array. You'll need Perl v5.14 to use the s///r. There's similarities to other solutions using a Schwartzian Transform.
#!/usr/bin/env perl use 5.014; use warnings; my $data = [ { Color => 'black', Date => '01-05-2019' }, { Color => 'blue', Date => '10-11-2020' }, { Color => 'green', Date => '08-06-2022' }, { Color => 'white', Date => '01-03-2022' }, { Color => 'red', Date => '03-21-2021' }, ]; my @sorted = map $_->[1], sort { $b->[0] <=> $a->[0] } map [$_->{Date} =~ s/(..)-(..)-(....)/$3$1$2/r, $_], @$data; use Data::Dump; dd $sorted[0];
Output:
{ Color => "green", Date => "08-06-2022" }
— Ken
|
---|
In Section
Seekers of Perl Wisdom