Moritz has the answer to your question.

I have some concerns about your approach.

The formats you list,

min:sec,frac e.g. "5:29,11" sec,frac e.g. "29,11" sec e.g. "29" ,frac e.g. ",11" min:sec e.g. "5:29" someth. else e.g. "abc"

are not normalized. In consequence, you provide no coverage of possibilities such as 5:29,3, which appears to be at least theoretically possible in your schema. Were you to begin by normalizing the data to mm:ss,frac, writing a reliable conversion to seconds would be both more reliable and easier (and which is left as an exercise for the user).

That said, and accepting the un-normalized data, suhailck's solution has much to commend it, but also has a couple glitches:

  1. Its initial test of the string won't correctly handle mixed alphanumerics, such as 1abc
  2. Its use of $3/10**2 in calculation of the decimal portion fails to consider the possiobility (again, not in your schema, but, ISTM, distinctly possible) of a single-digit, decimal fraction, such as .3 or a decimal fraction where length > 2, as, for example, .303

So, in part TIMTOWTDI, and in part, perhaps by way of improvement:

#!/usr/bin/perl use strict; use warnings; # 855222 # 1) min:sec,frac e.g. "5:29,11" # 2) sec,frac e.g. "29,11" # 3) sec e.g. "29" # 4) ,frac e.g. ",11" # 5) min:sec e.g. "5:29" # 6) someth. else e.g. "abc" my $seconds; my @times=("5:29,11", "29,11", "29:00", "29:00,3", "29:00,303","29:13, +07", ",11", "0:11", "5:29", "abc", "1abc"); for my $time(@times) { no warnings 'uninitialized'; if ($time =~ /[a-z]/i) { # won't catch non-"time-ish" symbols s +uch as ?, #, @, etc # $seconds = -1; # my personal preference is for an explicit er +ror statement print "Data not recognized: $time\n"; next; } else { ($seconds = $time) =~ s<^(?:(\d+):)?(\d+)?(?:,(\d+))?$><$1*60+$2+$ +3/(10**length($3))>e; } print "$time = $seconds \n"; }

which produces:

5:29,11 = 329.11 29,11 = 29.11 29:00 = 1740 29:00,3 = 1740.3 29:00,303 = 1740.303 29:13,07 = 1753.07 ,11 = 0.11 0:11 = 11 5:29 = 329 Data not recognized: abc Data not recognized: 1abc

In reply to Re: Convert time string to seconds by ww
in thread Convert time string to seconds by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.