shadkeene
09-19-2007, 03:36 AM
Hi,
I'm parsing multiple lines of data and eventually will be comparing values. However, I've run into some difficulty looping through two sets of data. Each set should be an array of three weather observations. However, I'm only able to extract the first line's wind direction instead of three. Therefore, I end up with two wind directions instead of six.
I used the linelen function to determine if my attempt to split the string into three arrays (3 different weather observations) was failing, and it seems to be. I'm splitting on new line character, so I'm wondering what's wrong?
Another issued I just ran into is using the "|" operator for matching one pattern or another. Sometimes winds are in the format 27015KT and other times 27015G25KT. So I tried to match both patterns...only working for the first, not the second. Thanks for any help.
Here's my code:
#!/perl/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use LWP::Simple;
use HTML::TokeParser::Simple
print header;
print start_html("WindshftObs");
my $sjc="sjc";
my $sfo="sfo";
my $sql="sql";
my @data = Winds($sfo, $sjc, $sql);
print "@data";
sub Winds
{
return "Error: No argument sent to Winds" unless @_;
my @apt = @_;
my @data;
foreach my $icao (@apt) {
my $url = "http://www.wrh.noaa.gov/mesowest/getobext.php?wfo=&sid=K$icao&num=3&raw=3&dbn=m&banner=off";
my $content= get($url) or die "Error getting file: $!";
my @lines = split(/'\n'/, $content);
my $linelen = scalar(@lines);
print "$linelen<br>";
foreach my $line (@lines) {
print "$line<br><br>";
if ($line =~ (/(\d{3})+\d{2}KT|(\d{3})+\d{2}G\d{2}KT/)) {
push @data, $1;
}
}
}
return @data;
}
print end_html;
And my results are:
1
KSFO 190056Z 26018G26KT 10SM FEW014 16/09 A2993 AO2 PK WND 27029/0027 SLP135 T01610094
KSFO 182356Z 26017G28KT 10SM FEW014 17/09 A2994 AO2 PK WND 26028/2349 SLP137 T01670094 10200 20167 56008
KSFO 182256Z 26014KT 10SM FEW014 17/10 A2995 AO2 SLP141 T01720100
1
KSJC 190053Z 34016KT 10SM FEW026 19/10 A2991 AO2 PK WND 34027/0024 SLP126 T01940100
KSJC 182353Z 33016G21KT 10SM FEW026 22/11 A2990 AO2 SLP124 T02170106 10250 20189 56014
KSJC 182253Z 32011KT 10SM CLR 23/09 A2991 AO2 SLP127 T02330094
1
KSQL 190047Z 25014G22KT 10SM SKC 17/10 A2993
KSQL 182347Z 27020G25KT 10SM SKC 18/10 A2992
KSQL 182247Z 29014KT 10SM SKC 21/09 A2992
340
Only get one value because regex is not getting the 25014G22KT pattern, and the three lines appear to be one string, not an array of three strings. Thanks again for any help,
Shad
I'm parsing multiple lines of data and eventually will be comparing values. However, I've run into some difficulty looping through two sets of data. Each set should be an array of three weather observations. However, I'm only able to extract the first line's wind direction instead of three. Therefore, I end up with two wind directions instead of six.
I used the linelen function to determine if my attempt to split the string into three arrays (3 different weather observations) was failing, and it seems to be. I'm splitting on new line character, so I'm wondering what's wrong?
Another issued I just ran into is using the "|" operator for matching one pattern or another. Sometimes winds are in the format 27015KT and other times 27015G25KT. So I tried to match both patterns...only working for the first, not the second. Thanks for any help.
Here's my code:
#!/perl/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use LWP::Simple;
use HTML::TokeParser::Simple
print header;
print start_html("WindshftObs");
my $sjc="sjc";
my $sfo="sfo";
my $sql="sql";
my @data = Winds($sfo, $sjc, $sql);
print "@data";
sub Winds
{
return "Error: No argument sent to Winds" unless @_;
my @apt = @_;
my @data;
foreach my $icao (@apt) {
my $url = "http://www.wrh.noaa.gov/mesowest/getobext.php?wfo=&sid=K$icao&num=3&raw=3&dbn=m&banner=off";
my $content= get($url) or die "Error getting file: $!";
my @lines = split(/'\n'/, $content);
my $linelen = scalar(@lines);
print "$linelen<br>";
foreach my $line (@lines) {
print "$line<br><br>";
if ($line =~ (/(\d{3})+\d{2}KT|(\d{3})+\d{2}G\d{2}KT/)) {
push @data, $1;
}
}
}
return @data;
}
print end_html;
And my results are:
1
KSFO 190056Z 26018G26KT 10SM FEW014 16/09 A2993 AO2 PK WND 27029/0027 SLP135 T01610094
KSFO 182356Z 26017G28KT 10SM FEW014 17/09 A2994 AO2 PK WND 26028/2349 SLP137 T01670094 10200 20167 56008
KSFO 182256Z 26014KT 10SM FEW014 17/10 A2995 AO2 SLP141 T01720100
1
KSJC 190053Z 34016KT 10SM FEW026 19/10 A2991 AO2 PK WND 34027/0024 SLP126 T01940100
KSJC 182353Z 33016G21KT 10SM FEW026 22/11 A2990 AO2 SLP124 T02170106 10250 20189 56014
KSJC 182253Z 32011KT 10SM CLR 23/09 A2991 AO2 SLP127 T02330094
1
KSQL 190047Z 25014G22KT 10SM SKC 17/10 A2993
KSQL 182347Z 27020G25KT 10SM SKC 18/10 A2992
KSQL 182247Z 29014KT 10SM SKC 21/09 A2992
340
Only get one value because regex is not getting the 25014G22KT pattern, and the three lines appear to be one string, not an array of three strings. Thanks again for any help,
Shad