Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Jan 18, 2022 at 18:18 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found