Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by kcott (Bishop)
on Jan 19, 2022 at 06:45 UTC ( #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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-28 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?