in reply to Re^4: 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.
If you're sorting $a cmp $b the most recent will be in the last element rather than the first so pull out $sorted[-1] instead. The alternative is to swap the order of comparison ($b cmp $a) and then the most recent will be first.
#!/usr/bin/env perl use 5.032; my @dates = qw( 02-03-2003 03-01-2015 01-15-2022 ); my @sorted = sort { my $new_a = join( q{-}, (split(/-/,$a))[2,0,1] ); my $new_b = join( q{-}, (split(/-/,$b))[2,0,1] ); $new_b cmp $new_a } @dates; say $sorted[0]; __END__ $ perl blonk.plx 01-15-2022
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Get most recent data based on a date from an array of hashes.
by Anonymous Monk on Jan 19, 2022 at 17:52 UTC | |
by Fletch (Bishop) on Jan 19, 2022 at 18:19 UTC |
In Section
Seekers of Perl Wisdom