I have a requirement to parse the etime field of solaris ps command. It looks like this (from man ps) [[dd-]hh:]mm:ss The leading two fields (days and hours) are optional. My first attempt was a re which looks pretty nasty:
for ($etime){ my ($dd,$hh,$mm,$ss)=() # see man ps etime looks like [[dd-]hh:]mm:ss if ( /^(?:(?:(\d{1,2})-){0,1})(?:(?:(\d{2}):){0,1})(\d{1,2}):(\d{2}) +$/ ){ $dd = $1 || ''; $hh = $2 || ''; $mm = $3; $ss = $4; $etime_secs= $ss+($mm*60)+($hh*60*60)+($dd*24*60*60); } }
I am sure the re can be improved but really the problem seemed to be begging for a split:
for ($etime){ # see man ps etime looks like [[dd-]hh:]mm:ss # if either $dd and $hh are not present then they will eat # up the $mm & $ss so split on a non digit and reverse it: my ($ss, $mm, $hh, $dd)= reverse split /\D/; $hh += 0; # stop warning as $hh may not be initialised $dd += 0; # same as $hh :) $etime_secs= $ss+($mm*60)+($hh*60*60)+($dd*24*60*60); }
Which is much nicer though I am just itching to get rid of the lines to make sure $hh and $dd are initialised :) Can any-one see a better way?

--
Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth


In reply to 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.