Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Find maximum (Activity) date from array hash.

by Sami_R (Sexton)
on Jun 02, 2020 at 19:54 UTC ( #11117606=perlquestion: print w/replies, xml ) Need Help??

Sami_R has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I have a list of array hash

my @array_data;

for example: when I loop through I get

foreach my $hash (@array_data) { $logger->log( "HASH NOW: =====> ". Dumper($hash)); }

I get…

HASH NOW: =====> $VAR1 = { 'Name' => 'First Segment', 'Balance' => '183.57', 'days' => 0, 'First Date' => '12/02/2020', 'payment' => 0, 'Activity Date' => '07/05/2020', 'Total Commission' => 0, }; HASH NOW: =====> $VAR1 = { 'Name' => 'Discontinue Segment', 'Balance' => '67.14', 'days' => 3, 'First Date' => '17/02/2020', 'payment' => 0, 'Activity Date' => '27/03/2020', 'Total Commission' => 0.12, }; HASH NOW: =====> $VAR1 = { 'Name' => 'Last Segment', 'Balance' => '12.56', 'days' => 7, 'First Date' => '19/03/2020', 'payment' => 0, 'Activity Date' => '21/04/2020', 'Total Commission' => 0, };

and so on ...

Expected output:

my $activity_date = $hash->{'Activity Date'}; print “Activity date: $activity_date”;

output: Activity date: 07/05/2020

Have taken previous examples from PerlMonks tried this:

my $max_date = 0; $_ > $max_date and $max_date = $_ for values @array_data;

Terribly I am doing wrong by not using the key (I couldn't able to get the desired output, sorry don't know), please give me directions to get the maximum date from the array list. Thank you,

Replies are listed 'Best First'.
Re: Find maximum (Activity) date from array hash.
by hippo (Chancellor) on Jun 02, 2020 at 20:27 UTC

    You can use Time::Piece to perform the date comparisons. Then it's just a matter of choosing a sensible initial value to compare against. You could use whatever is first in your array but I've chosen to use the epoch here:

    use strict; use warnings; use Test::More tests => 1; use Time::Piece; my @aoh = ( { 'Name' => 'First Segment', 'Balance' => '183.57', 'days' => 0, 'First Date' => '12/02/2020', 'payment' => 0, 'Activity Date' => '07/05/2020', 'Total Commission' => 0, }, { 'Name' => 'Discontinue Segment', 'Balance' => '67.14', 'days' => 3, 'First Date' => '17/02/2020', 'payment' => 0, 'Activity Date' => '27/03/2020', 'Total Commission' => 0.12, }, { 'Name' => 'Last Segment', 'Balance' => '12.56', 'days' => 7, 'First Date' => '19/03/2020', 'payment' => 0, 'Activity Date' => '21/04/2020', 'Total Commission' => 0, } ); my $maxdate = localtime (0); for my $hr (@aoh) { my $date = Time::Piece->strptime ($hr->{'Activity Date'}, '%d/%m/% +Y'); $maxdate = $date if $date > $maxdate; } is $maxdate->dmy ('/'), '07/05/2020';

      Thank you hippo.

      Solution is simple and elegant! Thank you so much

Re: Find maximum (Activity) date from array hash.
by CountZero (Bishop) on Jun 02, 2020 at 20:08 UTC
    A few things:

    • You will have to dereference $_ : $_->{'Activity Date'}
    • = isn't a comparison operator, it is the assignment operator. Use == instead for numerical tests. But you really need eq to check if both dates are the same.
    • Finally, to see if one date is before or after another, you cannot use > or <. At least not if you use this date-format. The ISO date format (YYYY-MM-DD) can do that more easy. So either transform your date format into the ISO format of use one of the many Date or DateTime modules on CPAN.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Thank you CountZero

      Based on your notes, have used hippo's code with ISO date format (YYYY-MM-DD) and compared the date like

      $maxdate = $date if $date gt $maxdate;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11117606]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2020-10-20 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (209 votes). Check out past polls.

    Notices?