Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Get most recent data based on a date from an array of hashes.

by kcott (Archbishop)
on Jan 19, 2022 at 06:45 UTC ( [id://11140602]=note: print w/replies, xml ) Need Help??


in reply to Get most recent data based on a date from an array of hashes.

TMTOWTDI,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found