in reply to Time to seconds

This sounds like a simple case of string munging
use strict; my $seconds = 0; my $timestr = '5H3M17S'; my ($hr,$min,$sec) = $timestr =~ /^(?:(\d+)H)?(?:(\d)+M)?(\d+)S$/; $seconds += $hr * 60 * 60 if defined $hr; $seconds += $min * 60 if defined $min; $seconds += $sec; print "seconds left to going live - $seconds";
Pretty simple stuff really, just grab what's there, do a few multiplications and your set.
HTH

broquaint

Replies are listed 'Best First'.
Re: Re: Time to seconds
by ariels (Curate) on Nov 15, 2001 at 20:10 UTC
    If you also want to test for validity, you might want to replace the regexp search
    $timestr =~ /^(?:(\d+)H)?(?:(\d)+M)?(\d+)S$/;
    with
    $timestr =~ /^(?:(?:(\d+)H)?(\d)+M)?(\d+)S$/;.

    The difference is that the first will accept 10H5S, while the second won't. (And you'll be testing that the match succeeds in real code, of course).

Re: Re: Time to seconds
by argus (Acolyte) on Nov 15, 2001 at 20:28 UTC
    Except that this doesn't work when there are no seconds.
    I.E. $timestr = '3M';

    However, the first one does.

    Thank you both, sometimes the simplest things elude us. :(