Here's a different approach, better, worse, useful to you is your call:)

#! perl -slw use strict; my $re_etime = qr[ (?{ our $ETIME = 0 }) (?: (\d{2}) (?{ $ETIME = 24 * $^N||0 }) - )? (?: (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N||0) }) : )? (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N) }) : (\d{2}) (?{ $ETIME += $^N }) ]x; for my $test ( '25:41', '19:25:41', '11-19:25:41' ) { our $ETIME; print "$test => $ETIME" if $test =~ $re_etime; } __DATA__ D:\Perl\test>261741 25:41 => 91541 19:25:41 => 69941 11-19:25:41 => 1020341

I hope the math is right:)

The regex explained

#! perl -slw use strict; my $re_etime = qr[ # Init a var to hold our value. (?{ our $ETIME = 0 }) # The whole line is optional # Capture 2 digits followed by - if present # What we captured $ETIME = ($^N) or 0 * 24 (?: (\d{2}) (?{ $ETIME = 24 * $^N||0 }) - )? # Optional \d{2}: if present, add them in, convert to seconds (?: (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N||0) }) : )? # Compulsory 2 digits , add em in and convert to seconds (\d{2}) (?{ $ETIME = 60 * ($ETIME + $^N) }) : # Compulsory colon # And the seconds. (\d{2}) (?{ $ETIME += $^N }) ]x; for my $test ( '25:41', '19:25:41', '11-19:25:41' ) { our $ETIME; # Gain access to our var #If the match succeeds, $ETIME has our value. print "$test => $ETIME" if $test =~ $re_etime; }

Personally, I find the uncommented version clearer and more readable. The comments just seems to muddy things up.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: parsing etime field from solaris ps by BrowserUk
in thread parsing etime field from solaris ps by greenFox

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.