in reply to Re: Getting times (weeks, days, hours, minutes, seconds)
in thread Getting times (weeks, days, hours, minutes, seconds)

Thanks to everyone who responded. I don't want to spam the place responding to everyone, so I hope you all don't mind just this one post. Also, thanks for showing me modules and functions that are new to me. I will look into them further if I need them later. For something as simple as this, I decided to just do a little math myself.

I hope that you all aren't disappointed that I just used return instead of printf in my subroutine. The whole print vs. printf conversation seems best left for a completely independent thread.

Leap seconds? Where have I been? :)

Looks like the -> may have something to do with objects and classes, which I am not versed in yet. I have read over several perldocs on objects and barely get it. I haven't dealt with objects in any code I have written. Objects seems to be another topic left to an independent thread. I think I would need to see a full example all together to begin to grasp the subject.

Again, thank you all for helping me out. Have some cookies!

use strict; use warnings; #I made my variables more semantic. @times was just to general, %durat +ions is a better word, thanks to those who used it. :) my %durations = ( 's1' => [qw(3:58 2:48 4:28 5:06 6:50 5:33 4:05 3:29 4:48 6:19)], 's2' => [qw(5:33 5:08 5:30 6:52 6:56 6:48 6:34 5:43 6:50 8:32 6:22 8 +:39)], 's3' => [qw(5:21 8:01 5:37 7:19 5:46 7:44 6:43 7:17 8:02 6:50 7:54 8 +:44)], ); # %durations are minutes and seconds. sub get_duration { my ($var) = @_; my $total_seconds; for my $duration (@{$durations{$var}}) { my ($minutes,$seconds) = split(':',$duration); # The change here m +ade the line much easier to read. :) $total_seconds += ($minutes * 60) + $seconds; # This eliminated my + need to create another array then adding the values of that array. : +) } my $total_minutes = int($total_seconds/60); #I totally forgot about +int. :S my $total_hours = int($total_minutes/60); my $print_seconds = $total_seconds % 60; my $print_minutes = $total_minutes % 60; return $total_hours.':'.$print_minutes.':'.$print_seconds."\n"; } print get_duration('s1'); print get_duration('s2'); print get_duration('s3');
Have a nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Getting times (weeks, days, hours, minutes, seconds)
by ikegami (Patriarch) on Jul 06, 2010 at 21:27 UTC

    The whole print vs. printf conversation seems best left for a completely independent thread.

    I didn't realise there was any controversy. It's not even a question of print vs printf but one of

    my ($h,$m,$s) = ...; $m = "0$m" if $m < 10; $s = "0$s" if $s < 10; my $dur = "$h:$m:$s";

    vs

    my ($h,$m,$s) = ...; my $dur = sprintf("%d:%02d:%02d", $h,$m,$s);

    Right now, you are creating timestamps like 1:3:43 instead of 1:03:43. printf and sprintf are quite apt at formatting this kind of data, but you don't have to use them. Take your pick.

    %d Signed 32/64-bit integer %2d ...space padded (on the left) to occupy (at least) 2 columns %02d ...zero padded (on the left) to occupy (at least) 2 columns

    hope that you all aren't disappointed that I just used return instead of printf in my subroutine.

    No, separation of calculation and I/O is a good thing. However,

    print get_duration('s1');

    should be

    print get_duration($durations{s1});

    or

    print get_duration(@{ $durations{s1} });

    (with the corresponding changes within get_duration). There's no reason for %durations to be global.

    Leap seconds? Where have I been? :)

    That post tries to show why DateTime wasn't a good solution for this problem, not how to solve the problem using DateTime. Sounds like it worked.

    I have read over several perldocs on objects and barely get it

    In short,

    • Objects represent some object (e.g. a specific user, a specific window, a specific timestamp, etc).
    • A type of object (e.g. user, window, timestamp) is called a class.
    • Objects contain data about that object ("attributes") and the class provides functions to manipulate the objects ("methods").
    • Methods can provide access to the attributes of an object (e.g. my $title = $window->get_title() and $window->set_title($title);)
    • Methods can provide cause actions to be performed. (e.g. $window->close();)