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

foreach my $ids (@id_list){ print "$ids-->$date-->$time\n"; } output: real-->2004-11-10-->65519 real-->2004-11-10-->77777 real-->2004-11-10-->33333
How to add the condition like, if date is greater, then print only that row.
if date, is same for the three then, check for time and print only that row.
For example: I have to print "real-->2004-11-10-->77777"

Replies are listed 'Best First'.
Re: check for date and time
by colwellj (Monk) on Feb 26, 2010 at 05:04 UTC
    I assume you mean that you have to print the latest time for each date.
    Why not put the dates in a hash with the time as a value.
    When you get an already defined hash key you can check to see if the time is later and update it.
    Then, print out you list of hash and keys with whatever formatting you want.
      How to create a hash of id as key, value with the latest date and time.
        try something like;
        #!/usr/bin/perl -w use strict; my %idhash; my @ids; foreach (@id_list){ #split up your input line into something useful @ids = split (/-->/,$_); # check to see if that date is already in your hash and if it is ne +wer if(defined($idhash{$ids[1]}) && $idhash{$ids[1]} >= $ids[2]){ next }else{ #populate your hash $idhash{$ids[1]} = $ids[2]; } } #then print your dates from the hash using foreach key