in reply to Time to seconds

my $time = "5H3M17S"; $time =~ /((\d+)H)?((\d+)M)?((\d+)S)?/; my $secs = 3600*$1 + 60*$3 + $5;

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re: Re: Time to seconds
by DrManhattan (Chaplain) on Nov 15, 2001 at 20:29 UTC
    I haven't golfed in a while, so here's Masem's code in one line:
    (my $secs = "5H3M17S") =~ s/((\d+)H)?((\d+)M)?((\d+)S)?/3600*$1 + 60*$ +3 + $5/e;
    And just to be different:
    use strict; my $time = "5H3M17S"; my $secs = 0; my $exponent = 0; map { $secs += $_ * 60 ** $exponent++ } reverse(split(/[HMS]/, $time)) +;

    -Matt

Re: Re: Time to seconds
by frankus (Priest) on Nov 15, 2001 at 20:33 UTC
    Hmm donuts^H^H^H^H^H^H golf..... ;o)

    $time=~s/(\d+)(?=[HMS])/($secs*=60)+=$1;$1/eg;

    --

    Brother Frankus.

    ¤