Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Fletch (Bishop)
on Jan 18, 2022 at 18:45 UTC ( #11140576=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.

Your sort call just reorders your list, it does nothing to "filter" the contents. You want to take the first last item off the generated list.

Also it doesn't make much sense to use s/// to modify the string to M-D-Y to then return a different string Y-M-D. Better would be to either use just a match m// and return the new string, or to actually switch things around with the s/// and return the modified string.

## Either $mdy =~ m{^(...blahblah...)}; return "$3-$1-$2"; ## Or . . . $mdy =~ s{^(...blahblah...)}{$3-$1-$2}; return $mdy; ## Or maybe (with a new enough perl) return $mdy =~ s{...}{$3-$1-$2}r;

Edit: Misread the order of comparison being used in the parent sample code.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: Get most recent data based on a date from an array of hashes.
by Anonymous Monk on Jan 18, 2022 at 19:10 UTC
    Yes, but still cant get the result of the most recent date from the array:
    $mdy =~ m{^(\d{1,2})\D(\d{1,2})\D(\d{4}|\d{4}\s+)}; return "$3-$1-$2";

      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.

        I liked your approach to this, just one question, the code works with the dates in MM-DD-YYYY and YYYY-MM-DD, is this on this part of the code "2,0,1", that's the part I am confused with it, thanks!
        #!/usr/bin/env perl #use 5.032; use strict; use warnings; use Data::Dumper; my @dates = qw( 02-03-2003 03-01-2022 01-15-2022 01-15-2023 01-15-2001 + 11-15-2023); #my @dates = qw( 2003-02-03 2015-03-01 2032-01-15 2023-01-15 2001-01-1 +5 2028-11-15); print "\n\n ".Dumper \@dates; 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; print "\n\n $sorted[0]\n\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-05-27 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (96 votes). Check out past polls.

    Notices?