in reply to time accumulater
Are you confident that this final chunk of each line always has 2 colons and three sets of digits -- i.e. 10 seconds is sure to be rendered as "00:00:10" (or "0:00:10" or "0:0:10")?
And do you really want the output to be expressed in minutes and seconds (even when the total may be several hours)?
If you answered yes to all the above, then your code could be a lot shorter:
In fact, you could eliminate the "@words" array in the for loop:use strict; my @lines0; # using your array name # ...do whatever you do to fill @lines0... my $totalsec = 0; for ( @lines0 ) { my @words = split /\s+/; my ( $hrs, $mins, $secs ) = split /:/, pop @words; $totalsec += ( $hrs * 60 + $mins ) * 60 + $secs; } printf( "Total time is %d minutes and %d seconds\n", int( $totalsec / 60 ), $totalsec % 60 );
Now, if you have to deal with "deficient" strings (i.e. "00:10" or just "10"), you need to know how to resolve these, and adjust the code accordingly (or, if they occur and you don't know how to resolve them, you have to trap those problem lines somehow -- either die, or issue a warning about ignoring unusable input and keep going).for ( @lines0 ) { my ( $hrs,$mins,$sec ) = split( /:/, pop( split /\s+/ )); $totalsec += ... }
If really large totals should be expressed in hours, minutes and seconds, I think it should be clear how to do that.
|
|---|