[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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


0
Richard Kreidl
Asked:
Richard Kreidl
  • 4
  • 2
  • 2
  • +1
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
print map{/ (.*)/,"\n"} (
     sort
     map{my($h,$m)=/(\d+):\d+(\w+)/;sprintf"%s%d %s",$m,$h%12,$_}
     $TNC, $RISK, $CTMHM
  )[-1];
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now