Strings to evaluate latest time?

I have three strings that I need to check the latest time out of the three:
4:15am   5:13am   4:06am
3:36am   4:49am   3:33am
1:19am   2:43am   1:17am

Example output:
5:13am
4:49am
2:43am

Now, there could be instances where one or more of the three strings is all characters:
4:15am   running   4:06am
3:36am   4:49am   bypassed
3:36am   running   bypassed

Example output:
4:15am
4:49am
3:36am

thanks


LVL 2
Richard KreidlSoftware DeveloperAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
print map{/ (.*)/,"\n"} (
     sort
     map{my($h,$m)=/(\d+):\d+(\w+)/;sprintf"%s%d %s",$m,$h%12,$_}
     $TNC, $RISK, $CTMHM
  )[-1];
0
 
ozoCommented:
while( <DATA> ){
  print( (sort/(\d+:\d+\w+)/g)[-1] ,"\n");
}
__DATA__
4:15am   5:13am   4:06am
3:36am   4:49am   3:33am
1:19am   2:43am   1:17am
4:15am   running   4:06am
3:36am   4:49am   bypassed
3:36am   running   bypassed
0
 
Suhas .QA ManagerCommented:
open(IN,"time.txt");
while(<IN>)
{
    @arr = split('\s+',$_);
    @resulted = sort { $b <=> $a } @arr;
    $greatest_time = shift(@resulted);
    print "$greatest_time\n";
}



0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ozoCommented:
while( <DATA> ){
  print map{/ (.*)/,"\n"} (
     sort
     map{my($h,$m)=/(\d+):\d+(\w+)/;sprintf"%s%d %s",$m,$h%12,$_}
     /(\d+:\d+\w+)/g
  )[-1];
}
__DATA__
12:12am  2:23pm   12:13pm
9:12am   12:23pm   11:13pm
12:12am  2:23am   12:13pm
9:12am   12:23am   2:13am
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
ozo, it works, but how would I use this in a script in which I bringing in the three strings from a textfile??

Example:
while (<INPUT>)
      {
chomp($_);
my ($TNC, $RISK, $CTMHM) = split(/\|/, $_);
}
0
 
Suhas .QA ManagerCommented:
open(INPUT,"time_file.txt"); # in current directory where the time_file.txt exist or you have to mention full #path of file

while(<INPUT>)
{
....
}


0
 
Richard KreidlSoftware DeveloperAuthor Commented:
suhasbharadwai, Your solution isn't quite working correctly.

the data is below

4:15am   5:13am   4:06am
3:36am   4:49am   3:33am
1:19am   2:43am   1:17am
4:15am   running   4:06am
3:36am   4:49am   bypassed
3:36am   running   bypassed


Output below:
5:13am
4:49am
2:43am
4:06am
4:49am
3:36am


Look at the 4th entry the latest time should be: 4:15am, not 4:06am

0
 
ozoCommented:
maps are not necessary if all times are before 10am
0
 
DropZoneCommented:
Try the following script:

#! /usr/bin/perl

use strict;

# This represents the array of the timestrings
my @timestrings =
    (
    '4:15am   5:13am   4:06am',
    '3:36am   4:49am   3:33am',
    '1:19am   2:43am   1:17am',

    '4:15am   running   4:06am',
    '3:36am   4:49am   bypassed',
    '3:36am   running   bypassed'
    );

# This array will contain the higher times found on each time string
my @hightimes  = ();

# Process each timestring
foreach my $timestr (@timestrings)
    {
    my @times   = split(/\s+/, $timestr); # split on whitespace
    my $highval = 0;
    my $highstr = '';

    foreach my $time (@times)
        {
        # Extract the time particles
        my ($hh, $mm, $ampm) = ($time =~ /^(\d\d?):(\d\d)(am|pm)$/);
        next unless ($hh && $mm && $ampm);
        $hh += 12 if ($ampm eq 'pm');  # adjust pm times

        my $timeval = ($hh.$mm) + 0; # get the numerical value

        # Check if this one is the highest so far
        if ($timeval > $highval)
            {
            $highval = $timeval;
            $highstr = $time;
            }
        }

    # add the highest one found in the current timestring (if any)
    push(@hightimes, $highstr) if ($highstr);
    }

# Display the highest times found
foreach my $highstr (@hightimes)
    {
    print "$highstr\n";
    }



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.