Programming challenge

Paul Seamons paul at seamons.com
Thu Sep 27 16:35:42 MDT 2007


Before I head home, here is a couple of modified choices as well...

# nasty one liner

my $a = "1 + 2:30 - 0:45:30.2 - 1:40.243";
$a =~ s/\b(\d+):(\d+(?:\.\d+)?)(?::(\d+(?:\.\d+)?))?/defined($3)?
$3+60*($2+60*$1):$2+60*$1/ge;
print "($a)\n";



# handle am/pm as well

my $b = "1 + 2:30pm - 0:45:30.2 A.M. - 1:40.243";


$b =~ s{
  \b (\d+)                    # opening hour or minute
    : (\d+ (?:\.\d+)?)        # minute or second (decimals optional)
    (?:
       : (\d+ (?:\.\d+)?)     # seconds (if present) (decimals optional)
    )?
    (?i: \s* ([ap])\.?m\.?)? # optional am/pm
}{
    my $pm = (defined($4) && lc($4) eq 'p' && $1 != 12) ? 12 : 0;
    defined($3) # had seconds ?
        ? $3 + 60 * ($2 + 60 * ($1 + $pm))
        : $2 + 60 * $1 + 3600 * $pm
}xge;

print "($b)\n";



More information about the PLUG mailing list