奇科電腦 回奇科電腦首頁奇科電腦LPI課程資訊奇科電腦Embedded課程資訊奇科電腦Cisco課程資訊奇科電腦Programming課程資訊奇科電腦Smartphone App課程資訊
Tech Tips18  Using Perl to analyze weather reports and find out the date and time of lost records?

Author: Ben|Date: 2009/04/06|Back to Tech Tips

The weather report is recorded every half hour, sometimes it didn’t make the record to the report. Please write a Perl program to find out the date and time of lost records.

Part of the weather report:
The date and time of lost records is from 0600-1100 on 19th day of year 2007.

:
:
:
2007 19 30 3.3556 231.84 ... ... ...
2007 19 100 3.8435 235.78999 ... ... ...
2007 19 130 4.0448 251.03 ... ... ...
2007 19 200 3.9693 255.75999 ... ... ...
2007 19 230 3.5651 256.70999 ... ... ...
2007 19 300 3.3964 253.72 ... ... ...
2007 19 330 2.4783 247.63 ... ... ...
2007 19 400 1.7784 232.53999 ... ... ...
2007 19 430 1.4422 26.17 ... ... ...
2007 19 500 1.8155 219.75 ... ... ...
2007 19 530 1.7307 226.56 ... ... ...
2007 19 1130 2.9181 301.48999 ... ... ...
2007 19 1200 2.6842 312.23001 ... ... ...
2007 19 1230 3.6911 312.17999 ... ... ...
2007 19 1300 3.3554 300.35001 ... ... ...
2007 19 1330 3.2761 302.17999 ... ... ...
2007 19 1400 3.5768 307.17001 ... ... ...
2007 19 1430 3.0593 294.85001 ... ... ...
2007 19 1500 3.4612 298.82001 ... ... ...
2007 19 1530 4.8399 301.17999 ... ... ...
2007 19 1600 4.6962 298.35999 ... ... ...
2007 19 1630 4.5457 307.51001 ... ... ...
2007 19 1700 4.5081 303.60999 ... ... ...
2007 19 1730 5.1268 272.78 ... ... ...
2007 19 1800 4.3762 99.63 ... ... ...
2007 19 1830 5.6355 287.67001 ... ... ...
2007 19 1900 4.8222 51.7 ... ... ...
2007 19 2000 4.3726 278.56 ... ... ...
2007 19 2030 6.5631 289.76001 ... ... ...
2007 19 2100 4.7712 289.23001 ... ... ...
2007 19 2130 4.8486 291.09 ... ... ...
2007 19 2200 4.0298 300.22 ... ... ...
2007 19 2230 5.1309 304.32001 ... ... ...
2007 19 2300 1.0221 333.35001 ... ... ...
2007 19 2330 1.2376 312.17001 ... ... ...
2007 19 2400 1.3574 272.70999 ... ... ...
2007 20 30 3.3556 23 1.84 ... ... ...
2007 20 100 3. 8435 2 35.78999 ... ... ...
:

#!/usr/bin/perl
#
# 奇科電腦 ( http://www.geego.com.tw )
#
use Time::Local "timelocal_nocheck";

my $previous_time = undef;
my $current_time = undef;

while( <DATA> )
{
next if $_ =~ /^\s*$/;
chomp;
@time_data = ( split( /\s+/ ) )[ 0,1,2 ] ;
my $year = $time_data[ 0 ] - 1900;
my $year_day = $time_data[ 1 ] + 1;
my $hour = '';
my $min = '';
if ( $time_data[ 2 ] !~ /^\d\d$/ )
{
$time_data[ 2 ] =~ /^(\d+?)(\d\d)$/;
$hour = $1;
$min = $2;
}
else
{
$hour = 0 ;
$min = $time_data[ 2 ];
}

if ( ! defined( $previous_time ) )
{
$previous_time = timelocal_nocheck(0, $min, $hour, $year_day, 0, $year );
next;
}

$current_time = timelocal_nocheck( 0, $min, $hour, $year_day, 0, $year );

while ( ( $previous_time + 1800 ) != $current_time )
{
@data = ( localtime( $previous_time + 1800 ) )[5, 7, 2, 1 ];
$previous_time += 1800;
printf( "%4d %2d %02d%02d\n", $data[0]+1900, $data[1], $data[2],$data[3] );
}
$previous_time = $current_time;
}

__DATA__

2007 19 30 3.3556 231.84 ... ... ...
2007 19 100 3.8435 235.78999 ... ... ...
2007 19 130 4.0448 251.03 ... ... ...
2007 19 200 3.9693 255.75999 ... ... ...
2007 19 230 3.5651 256.70999 ... ... ...
2007 19 300 3.3964 253.72 ... ... ...
2007 19 330 2.4783 247.63 ... ... ...
2007 19 400 1.7784 232.53999 ... ... ...
2007 19 430 1.4422 26.17 ... ... ...
2007 19 500 1.8155 219.75 ... ... ...
2007 19 530 1.7307 226.56 ... ... ...
2007 19 1130 2.9181 301.48999 ... ... ...
2007 19 1200 2.6842 312.23001 ... ... ...
2007 19 1230 3.6911 312.17999 ... ... ...
2007 19 1300 3.3554 300.35001 ... ... ...
2007 19 1330 3.2761 302.17999 ... ... ...
2007 19 1400 3.5768 307.17001 ... ... ...
2007 19 1430 3.0593 294.85001 ... ... ...
2007 19 1500 3.4612 298.82001 ... ... ...
2007 19 1530 4.8399 301.17999 ... ... ...
2007 19 1600 4.6962 298.35999 ... ... ...
2007 19 1630 4.5457 307.51001 ... ... ...
2007 19 1700 4.5081 303.60999 ... ... ...
2007 19 1730 5.1268 272.78 ... ... ...
2007 19 1800 4.3762 99.63 ... ... ...
2007 19 1830 5.6355 287.67001 ... ... ...
2007 19 1900 4.8222 51.7 ... ... ...
2007 19 2000 4.3726 278.56 ... ... ...
2007 19 2030 6.5631 289.76001 ... ... ...
2007 19 2100 4.7712 289.23001 ... ... ...
2007 19 2130 4.8486 291.09 ... ... ...
2007 19 2200 4.0298 300.22 ... ... ...
2007 19 2230 5.1309 304.32001 ... ... ...
2007 19 2300 1.0221 333.35001 ... ... ...
2007 19 2330 1.2376 312.17001 ... ... ...
2007 19 2400 1.3574 272.70999 ... ... ...
2007 20 30 3.3556 23 1.84 ... ... ...
2007 20 100 3. 8435 2 35.78999 ... ... ...


Best Browse: 1024x768 Copyright 2010 GeeGo Systems, Ltd.
service@geego.com.tw|DL: 0800-296-296|3F., No.60, Zhulun St., Zhongshan Dist., Taipei City 104, Taiwan (R.O.C.)|02-2711-6373