Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing 2 strings 2

Status
Not open for further replies.

AlexDeMarco

Technical User
Feb 12, 2002
9
US
I have the 2 following strings:

17Sep0215382228650366661
and
17Sep02153850

As you see the first part is a date/time I need to be able to tell the time difference between the two.. Any ideas would be great..
thanks!

- Alex
 
What exactly is the format of this 'number'? It looks like DDMonthYYhhmmss... is that correct? How rigid is this format? Assuming the above format, try this
Code:
    if ( $datetime =~ /^
                        \d+     # Day
                        \D+     # Month
                        \d\d    # Year
                        (\d\d)  # Hour
                        (\d\d)  # Minute
                        (\d\d)  # Seconds
                        (\d*)   # Fractional seconds
                    $/x
        ) {

            my $time = $1 * 3600 + $2 * 60 + "$3.$4" ;
            print "time: $time\n";
    }
If the format is more rigid (i.e. the number of digits and letters is always the same for each field) then unpack would probably be more efficient.
Code:
    my ($day, $mon, $yr, $hr, $min, $sec, $frac) = unpack 'A2A3A2A2A2A2A*', $datetime;
    my $time = $hr * 3600 + $min * 60 + "$sec.$frac" ;
For either of these methods, after you have the times in seconds then you can just compare them numerically.

jaa
 
compliment jaa.
i'm confused about the 'x' after the second '/'
in the substitution statement. can you pls explain it? ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
OK I'll try to explain better:

17Sep0215382228650366661
Day-Month-Year-time - Randomnumbers

17Sep02153850
Day-Month-Year-time

Where the time ends on the first field needs to be chopped off, and then compared to the string below and I need to know how much time has elapsed. THe top string could change in length, but the first part is always day-month-year-time.

- Alex
 
The 'x' extends the syntax of the regex to allow the white space and comments internal to the regex. Otherwise, the regex would have to be expressed on one line like,

if ($datetime =~ /^\d+\D+\d\d(\d\d)(\d\d)(\d\d)(\d*)$/ ...

with no comments..... not as easy to see and understand... 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Code:
#!perl

# Day-Month-Year-time - Randomnumbers
$t1 = '17Sep0215382228650366661';
$time1 = substr($t1,7,6);
print "$t1\nFrom substr, time1 is $time1\n\n";

# Day-Month-Year-time
$t2 = '17Sep02153850';
$time2 = substr($t2,7,6);
print "$t2\nFrom substr, time2 is $time2\n\n";

$elapsed = abs($time1 - $time2);
print "1 - absolute value of ($time1 - $time2) = $elapsed\n";

undef $time1;
undef $time2;
undef $elapsed;

# Or, if single digit days don't have a leading zero,
if ($t1 =~ /\d+\w{3}\d\d(\d{6})/) { $time1 = $1; }
if ($t2 =~ /\d+\w{3}\d\d(\d{6})/) { $time2 = $1; }

if ($time1 && $time2 )
    {
    $elapsed = abs($time1 - $time2);
    print "2 - absolute value of ($time1 - $time2) = $elapsed\n";
    }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top