Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Jan 19, 2022 at 16:38 UTC ( [id://11140616]=note: print w/replies, xml ) Need Help??


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

Note that max_by() can also return multiple max's if it is used in list context.

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

Outputs:

( { acc => 1111, Color => "green", Date => "08-06-2022", Step => "Plat +form" }, { acc => 1111, Color => "white", Date => "08-06-2022", Step => "Plat +form" }, )

Replies are listed 'Best First'.
Re^3: Get most recent data based on a date from an array of hashes.
by Marshall (Canon) on Jan 20, 2022 at 10:12 UTC
    This is way ++cool.
    I wasn't aware of List::AllUtils or List::Utilsby in the List:: menagerie

    List::Utils
    List::MoreUtils
    List::AllUtils
    List::SomeUtils
    List::UtilsBy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found