Yes yes yes. This is what I was getting at when I said earlier (in the chatterbox) that the code looked "far too regular". ncw, you've done a nice job abstracting the problem.

Also, the use of sprintf there to add the leading 0 is much more perl-ish than the few odd snippets I've seen in this thread of:

$var = "0$var" if $var < 10;
Maybe it's because I first saw that weird-looking code in a Matt Wright script, and it's now tainted me. It scares me to think what other horrors people have now learned from Matt Wright scripts. {grin}

I think for me the problem is that the non-sprintf solution does not scale well. For example, what if you wanted three digits? Do you add another test?

$var = $var < 10 ? "00$var" : $var < 100 ? "0$var" : $var;
No, if I saw code like that in a code review, I'd flag that immediately with "this must be changed before deployment: unmaintainable".

So, get cozy with sprintf: it solves many problems (including the rounding problem discussed a few days ago).

-- Randal L. Schwartz, Perl hacker


In reply to RE: RE: Convert seconds into a formatted ddd:hh:mm:ss string by merlyn
in thread Convert seconds into a formatted ddd:hh:mm:ss string by husker

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.