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 TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 )

 



gregork
New User

May 4, 2013, 4:05 PM

Post #1 of 5 (309 views)
Perl TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 ) Can't Post

I'm working with a file in TextWrangler with around 500 clock times that look like this <t>00:01:39</t> or <t>01:36:30</t>

the <t> is the beginning of each line.

I'm trying to turn the clock format into seconds. So those examples would turn into <t>99</t> and <t>5790</t>

I know there's a pretty simple text filter that I could write in Perl but I'm new to the language and in over my head.

I'm thinking it'll be something like,
after three characters,
multiply the next two digit number by 3600,
then multiply the next two digit number after the ":" by 60,
then multiply the next two digit number after the ":" by 1,
and finally add them all together.

Does this seem way off?

Thanks so much for any help or direction,
Best,
Gregor


g4143
Novice

May 4, 2013, 4:43 PM

Post #2 of 5 (306 views)
Re: [gregork] Perl TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 ) [In reply to] Can't Post

I would try something like....


Code
#!/usr/bin/perl 

use warnings;
use strict;

while ( <DATA> )
{
chomp;
if ( /(\d\d):(\d\d):(\d\d)/ )
{
print "<t>", $1 * 3600 + $2 * 60 + $3, "</t>\n";
}
}

__END__
<t>00:01:39</t>
<t>01:36:30</t>


Or if you want to write the values to a new file...


Code
#!/usr/bin/perl 

use warnings;
use strict;

$^I = '.bak';

while ( <> )
{
s{(\d\d):(\d\d):(\d\d)}{$1 * 3600 + $2 * 60 + $3}e;
print;
}

__END__



(This post was edited by g4143 on May 4, 2013, 5:15 PM)


recruiter
User

May 4, 2013, 6:20 PM

Post #3 of 5 (284 views)
Re: [g4143] Perl TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 ) [In reply to] Can't Post

From what I understood from your post, your file is already formatted as <t>00:00:00</t> ?

If this is the case, here is an example of how you can read in the data and convert the hh:mm::ss -> seconds.

Sample times.txt file:


Code
<t>00:01:39</t>    
<t>00:02:39</t>
<t>00:03:39</t>
<t>00:04:39</t>
<t>00:05:39</t>
<t>00:06:39</t>



You could use File::Slurp to read in your file, you can find the documentation on CPAN.


Code
use strict;   
use warnings;

use File::Slurp qw( read_file );

my @data = read_file('times.txt' {chomp =>1});

my @times =
map {
my($h,$m,$s) = split /:/, $_;
$h*3600+$m*60+$s
} @data;

print "$_\n" for (@times);



(This post was edited by hwnd on May 4, 2013, 7:11 PM)


gregork
New User

May 5, 2013, 9:46 AM

Post #4 of 5 (261 views)
Re: [hwnd] Perl TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 ) [In reply to] Can't Post

Hey all,
Thanks so much for the help!

What I ended up with (which might be a bit of a Frankenstein piece of code) was this:


Code
#! /usr/bin/env perl  
use v5.12; use strict; use warnings;
#----------------------------------------

while ( <> ) {
if ( m{([[:blank:]]*<t>)(\d\d)(:)(\d\d)(:)(\d\d)(</t>)}i) {
my $hours = $2 * 3600;
my $min = $4 * 60;
my $sec = $6;
my $full = $hours + $min + $sec;
say "$1$full$7";
} else {
print; }
}


and saved it as a Text Filter in TextWrangler.
It seems to do the trick.

Once again,
Thanks so much for the help!


recruiter
User

May 5, 2013, 10:31 AM

Post #5 of 5 (256 views)
Re: [gregork] Perl TextFilter to turn clock format ( 01:36:30 ) into seconds ( 5790 ) [In reply to] Can't Post

Here's a way you can do this in less lines:


Code
  

#!/usr/bin/env perl

use strict;
use warnings;
use v5.12;

#-------------------

while (<>) {
if ( /<(.+?)>.+<\/\1>/i ) {
my ($h,$m,$s) = split /:/, $_;
say $h*3600+$m*60+$s;

#or in one line
#print map { my ($h,$m,$s) = split /:/; $h*3600+$m*60+$s } $_;
} else {
print;
}
}


 
 


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

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