Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by tybalt89 (Prior)
on Jan 18, 2022 at 18:18 UTC ( #11140575=note: print w/replies, xml ) Need Help??


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

#!/usr/bin/perl use strict; use warnings; use List::AllUtils qw( max_by ); my $data = [ { 'Color' => 'green', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '08-06-2022' }, { 'Color' => 'black', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '01-05-2019' }, { 'Color' => 'blue', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '10-11-2020' }, { 'Color' => 'white', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '01-03-2022' }, { 'Color' => 'red', 'Step' => 'Platform', 'acc' => '1111', 'Date' => '03-21-2021' }, ]; my $mostrecent = max_by { my @fields = split /-/, $_->{Date}; join '', @fields[2,0,1] } @$d +ata; use Data::Dump 'dd'; dd $mostrecent;

Outputs:

{ acc => 1111, Color => "green", Date => "08-06-2022", Step => "Platfo +rm" }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-05-29 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?