Thank you all for your answers.

Now I wrote the following code. It is a bit longer, but more readable I think. And so I'm sure that it really is doing what I want. And I also wrote a function which is doing the way back.

use strict; use warnings; my @time_str_list = ( "abc", "1abc", ":", ":,", "5:29,11", "29,11", "2 +9:00", "29:00,3", "29:00,303", "9:13,07", ",11", "0:11", "5:29" ); my $seconds; for my $time_str ( @time_str_list ) { &convertTimeStrToSeconds($time_str, \$seconds); next unless defined $seconds; print("$time_str\n"); print("$seconds\n"); &convertSecondsToTimeStr($seconds, \$time_str); next unless defined $time_str; print("$time_str\n"); print("\n"); } sub convertTimeStrToSeconds { my ($time_str, $ref_seconds) = @_; # 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" $$ref_seconds = undef; # 6) # remove leading and trailing whitespace characters $time_str =~ s/^\s+|\s+$//g; # valid time string if( ($time_str =~ m/^(?<min>\d+):(?<sec>\d+),(?<frac>\d+)$/) ||# 1 +) ($time_str =~ m/^(?<sec>\d+),(?<frac>\d+)$/) || # 2) ($time_str =~ m/^(?<sec>\d+)$/) || # 3) ($time_str =~ m/^,(?<frac>\d+)$/) || # 4) ($time_str =~ m/^(?<min>\d+):(?<sec>\d+)$/) ) # 5) { no warnings 'uninitialized'; # intention: undef treated as zer +o $$ref_seconds = ($+{'min'} * 60.0) + $+{'sec'} + ($+{'frac'}/( +10**length($+{'frac'}))); } } sub convertSecondsToTimeStr { my ($seconds, $ref_time_str) = @_; $$ref_time_str = undef; if( $seconds > 0 ) { my $min = int($seconds / 60.0); my $sec = $seconds % 60.0; my $frac = int(sprintf("%.2f", ($seconds - int($seconds))*100. +0)); $$ref_time_str = "$min:" if( $min > 0 ); $$ref_time_str .= sprintf("%02d", $sec) if ( ($min > 0) || ($s +ec > 0) ); $$ref_time_str .= ",$frac" if ( $frac > 0 ); } }

With the error handling you are right. To set $seconds to -1 is not that good. Error handling is in general for me a difficult thing. I'm not sure what is the best way. Now I'm setting the output parameter to undef. Is this good style? Of course I could print a error message. Or I could return undef. Let's assume this function would be part of an official module. What would be the best error handling?

Thank you and Greetings

Dirk


In reply to Re^2: Convert time string to seconds by Dirk80
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.