Here is another way... Simply, sum the minutes and seconds and use gmtime() to convert the total number of seconds to hours, minutes, seconds. This of course assumes some time delta from the epoch number of seconds(0) for each item. Which is fine in this case as we are measuring duration, not absolute date values.
The year and day of week, etc are all meaningless in this context so just throw them away. Leap seconds, leap years don't matter as long as we are talking about data like your example.
#!/usr/bin/perl -w
use strict;
my @times = 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 );
my $total_minutes;
my $total_seconds;
foreach my $time (@times)
{
my ($m,$s) = split(':',$time);
$total_minutes += $m;
$total_seconds += $s;
}
my ($h,$m,$s) = (gmtime($total_minutes*60+$total_seconds))[2,1,0];
print "$h hours, $m minutes, $s seconds";
#1 hours, 25 minutes, 18 seconds
The list slice isn't necessary, but I like to line up the left hand side vars so that they are in the order that I use them later. my ($s,$m,$h) would be fine without the slice.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.