CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
perl to convert from H:MM:SS.S to MM.MMMM

 



Coatman
Novice

Jun 18, 2013, 8:03 AM

Post #1 of 12 (550 views)
perl to convert from H:MM:SS.S to MM.MMMM Can't Post

I want to use perl to read in from a text file time information formatted as DOW YYYY-MM-DD H:MM:SS.S, and then output just decimal minutes MM.MMMM. Given the input text file

Code
Fri 2013-06-14 0:59:15.3 
Sat 2013-06-15 1:01:30.6
Sun 2013-06-16 1:03:45.9

I want to compute the decimal minutes values

Code
(60*0)+59+(15.3/60) = 59.2550 
(60*1)+01+(30.6/60) = 61.5100
(60*1)+03+(45.6/60) = 63.7600

and then output just

Code
(59.2550, 61.5100, 63.7600)



FishMonger
Veteran / Moderator

Jun 18, 2013, 8:13 AM

Post #2 of 12 (549 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

What have you tried?

What errors/warnings are you receiving?


Coatman
Novice

Jun 18, 2013, 8:20 AM

Post #3 of 12 (547 views)
Re: [FishMonger] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

I did the conversion successfully using bash, and am seeking guidance to do the conversion using perl.


FishMonger
Veteran / Moderator

Jun 18, 2013, 8:25 AM

Post #4 of 12 (546 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

Loop over the data and use the split function or a regex to extract the time field. Then use the split function to separate the values you need in the calculations.

Give it a try and post back if you need more assistance.


Coatman
Novice

Jun 18, 2013, 9:02 AM

Post #5 of 12 (543 views)
Re: [FishMonger] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

Here is one way to do the conversion with bash. I want to use perl to extract the HOUR, MINUTE, and SECOND values, and then do the math.
#! /bin/bash
# Convert from DOW YYYY-MM-DD H:MM:SS.S to MM.MMMM
while read LINE
do
echo "LINE=${LINE}" | awk '{print $3}'
HOUR=`echo "LINE=${LINE}" | awk '{print $3}' | cut -b1`
MINUTE=`echo "LINE=${LINE}" | awk '{print $3}' | cut -b3-4`
SECOND=`echo "LINE=${LINE}" | awk '{print $3}' | cut -b6-9`
MINUTES_DECIMAL=`echo "scale=4 ; (${HOUR}*60)+(${MINUTE})+(${SECOND}/60)" | bc`
echo "${MINUTES_DECIMAL}"
done < elapsed_01.txt
exit 0


FishMonger
Veteran / Moderator

Jun 18, 2013, 9:46 AM

Post #6 of 12 (535 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post


Quote
I want to use perl to extract the HOUR, MINUTE, and SECOND values, and then do the math.


What's stopping you?

Are you saying that you don't know Perl and just want someone to write it for you?


FishMonger
Veteran / Moderator

Jun 18, 2013, 10:57 AM

Post #7 of 12 (527 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

We are not a code writing service and I normally don't post solutions until the OP shows some effort in solving the problem, which you clearly have not done. However, I sometimes make an exception.

The output of your shell script is not the same as what you say you want, so I'm not sure what you really need/want.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my @minutes;
while(<>) {
my ($hr,$min,$sec) = /(\d+):(\d+):([\d.]+)/;
push @minutes, sprintf("%.4f", (60*$hr + $min + $sec/60));
}

$, = ',';
print "(@minutes)\n";


Executed as: convert.pl elapsed_01.txt


Laurent_R
Veteran / Moderator

Jun 18, 2013, 11:00 AM

Post #8 of 12 (526 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

To get the various part of the time you need, try this:


Code
my  ($hour, $min, $sec) = ($1, $2, $3) if $line =~ /(\d+):(\d+):(\d+\.\d?)/;


This will populate $hour, $min and $sec with the values you need. I leave it to you to do the rest which should be simple.


Coatman
Novice

Jun 18, 2013, 12:51 PM

Post #9 of 12 (520 views)
Re: [FishMonger] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

Thank you.
Adding a comma to the push line adds a necessary comma after each decimal minute value, but also adds an extra unnecessary comma at the end. Please advise how to eliminate the extra comma at the end?
push @minutes, sprintf("%.4f,", (60*$hr + $min + $sec/60))
(59.2550, 61.5100, 63.7650,)


FishMonger
Veteran / Moderator

Jun 18, 2013, 12:58 PM

Post #10 of 12 (518 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

Simple, just don't put that extra comma in the push statement.

If you don't want to change the $, list separator var like I did, then simply add a join statement to the print statement.


Code
print '(', join(', ', @minutes), ")\n";



Coatman
Novice

Jun 18, 2013, 1:11 PM

Post #11 of 12 (516 views)
Re: [FishMonger] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

Using print '(', join(', ', @minutes), ")\n";
the output I get is
$ ./convert_to_min_05.pl elapsed_01.txt
(,59.2550, 61.5100, 63.7650,)
How to eliminate the extra commas at the beginning end end?


FishMonger
Veteran / Moderator

Jun 18, 2013, 1:39 PM

Post #12 of 12 (514 views)
Re: [Coatman] perl to convert from H:MM:SS.S to MM.MMMM [In reply to] Can't Post

If you're using the sprintf statement that I provided, then your output indicates that the first and last elements of the @minutes array are empty. To fix that, you need to wrap the code in a conditional.


Code
while(<>) {  
if ( /(\d+):(\d+):([\d.]+)/ ) {
my ($hr,$min,$sec) = $1, $2, $3;
push @minutes, sprintf("%.4f", (60*$hr + $min + $sec/60));
}
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives