Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Formatting elapsed time

by Hofmator (Curate)
on Sep 06, 2001 at 17:34 UTC ( #110568=note: print w/ replies, xml ) Need Help??


in reply to Formatting elapsed time

Am I overlooking something or could your sprintf statements be written much simpler like this?

$res .= sprintf "%dw", $weeks if $weeks; $res .= sprintf "%dd", $days if $days; $res .= sprintf "%dh", $hours if $hours; $res .= sprintf "%dm", $minutes if $minutes; $res .= sprintf '%ds', $seconds;

Update: That was changed a little bit too much, as grinder correctly remarked ... nevertheless, my main concern were those ugly string interpolations, and I think this works:

$res .= sprintf "%dw", $weeks if $we +eks; $res .= sprintf "%dd", $days if $days or $we +eks; $res .= sprintf "%dh", $hours if $hours or $days or $we +eks; $res .= sprintf "%dm", $minutes if $minutes or $hours or $days or $we +eks; $res .= sprintf '%ds', $seconds;

-- Hofmator


Comment on Re: Formatting elapsed time
Select or Download Code
Re:x2 Formatting elapsed time
by grinder (Bishop) on Sep 06, 2001 at 17:38 UTC

    Yes they could, but it would change the behaviour of the routine. For instance, 86401 would produce 1d1s instead of 1d0h0m1s.

    Which may be what you want (ultra-compact!), but it wasn't what I needed.

    update: re your update: you're quite right. Adding the $res into the format string is a bit too clever by half. I completely overlooked .= . It's feedback like this that make Perl Monks such a useful site.

    --
    g r i n d e r

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://110568]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2015-07-05 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls