View Full Version : values not pushing to global variable/scheduled tasks
shadkeene
08-28-2007, 06:24 PM
Hello again...I've been implementing recent advice on subroutines and it's been allowing for much more efficient code. However, my recent attempt at it has left me scratching my head. Here's my code so far:
One thing that's confusing is when I print "@data" within the subroutine (after the push statement), the values I'm trying to extract show up. However, when I print "@data" in the main part of the code (after I call the subroutine with arguments), nothing is returned.
Thanks for any advice ahead of time.
Shad
#!/perl/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use DBI;
use LWP::Simple;
use HTML::TokeParser::Simple
print header;
print start_html("SLPTrend");
my $ksfo="ksfo";
my $ksac="ksac";
my $kacv="kacv";
my @data= Pressure2($ksfo,$ksac,$kacv); #call subroutine for SLP of various airports
print "$data[0]";
sub Pressure2 {
return "Error: No argument sent to Pressure2" unless @_;
my @apt = @_;
foreach my $icao (@apt) {
my $url = "http://www.atmos.albany.edu/cgi/sa-cgi?$icao";
my $content = get($url) or die "Error getting file: $!";
my $p = HTML::TokeParser::Simple->new( \$content );
while ( my $token = $p->get_token ) {
next unless $token->is_text;
my $line = $token->as_is;
my $slpdata=$line;
my @SLP = split(/\s+/, $slpdata);
use List::Util qw(first);
my $element = first { /\d{4}/ } @SLP; #extracts first element with 4 consecutive numbers(SLP)
push @data, $element;
}
}
return $data[0], $data[1], $data[2];
}
shadkeene
08-28-2007, 08:08 PM
the other thing I meant to ask was how can I run a cgi script as a scheduled task in windows xp? I've tried to just run the .cgi file as a scheduled task, but that just opens up the .cgi text document window. I'd like the program to actually run and extract data/place in mysql at a certain time if I'm not at work. Maybe I'd just run the program as a .pl file? And if so, would I just rename my .cgi file to a .pl file? Thanks,
Shad
FishMonger
08-28-2007, 10:23 PM
Why did you remove the my @data; statement from the subroutine? Put it back in and see what happens.
FishMonger
08-28-2007, 10:28 PM
the other thing I meant to ask was how can I run a cgi script as a scheduled task in windows xp? I've tried to just run the .cgi file as a scheduled task, but that just opens up the .cgi text document window. I'd like the program to actually run and extract data/place in mysql at a certain time if I'm not at work. Maybe I'd just run the program as a .pl file? And if so, would I just rename my .cgi file to a .pl file? Thanks,
Shad
If you're not running the script via an interactive browser session, it doesn't make any sense to write it as a cgi script. Changing the ext from .cgi to .pl doesn't make any difference as far as cgi scripting is concerned. Using .cgi only helps the user to know the context in which the script is to be used.
shadkeene
08-29-2007, 02:53 PM
Thanks, I got the scheduled task to run, and I don't know why I took out the declaration within the subroutine...I'll put it back in.
Shad
shadkeene
08-29-2007, 04:46 PM
Fishmonger,
I put the my @data; statement back in the subroutine. However, still not getting any returns from the print "@data"; in the main code. I only get values when I use a print statement at the end of the foreach loop.
I can only think that the main code is not referencing the @data that was declared in the subroutine. Thanks for any help. Here's the code.
Shad
#!/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("SLPTrend");
my $ksfo="ksfo";
my $ksac="ksac";
my $kacv="kacv";
my @data= Pressure2($ksfo,$ksac,$kacv); #call subroutine for SLP and define Onshore and Offshore Gradients
print "@data";
sub Pressure2 {
return "Error: No argument sent to FcstPress2" unless @_;
my @apt = @_;
my @data;
foreach my $icao (@apt) {
my $url = "http://www.atmos.albany.edu/cgi/sa-cgi?$icao";
my $content = get($url) or die "Error getting file: $!";
my $p = HTML::TokeParser::Simple->new( \$content );
while ( my $token = $p->get_token ) {
next unless $token->is_text;
my $line = $token->as_is;
my $slpdata=$line;
my @SLP = split(/\s+/, $slpdata);
use List::Util qw(first);
my $element = first { /\d{4}/ } @SLP; #extracts first element with 4 consecutive numbers(SLP)
push @data, $element;
}
}
return $data[0], $data[1], $data[2];
}
print end_html;
FishMonger
08-29-2007, 05:20 PM
use Data::Dumper;
sub Pressure2 {
return "Error: No argument sent to FcstPress2" unless @_;
my @apt = @_;
my @data;
foreach my $icao (@apt) {
my $url = "http://www.atmos.albany.edu/cgi/sa-cgi?$icao";
my $content = get($url) or die "Error getting file: $!";
my $p = HTML::TokeParser::Simple->new( \$content );
while ( my $token = $p->get_token ) {
next unless $token->is_text;
my $line = $token->as_is;
my $slpdata=$line;
my @SLP = split(/\s+/, $slpdata);
use List::Util qw(first);
my $element = first { /\d{4}/ } @SLP; #extracts first element with 4 consecutive numbers(SLP)
push @data, $element;
}
}
print Dumper @data;
return $data[7], $data[15], $data[23];
}
FishMonger
08-29-2007, 05:31 PM
sub Pressure2 {
return "Error: No argument sent to FcstPress2" unless @_;
my @apt = @_;
my @pressure;
foreach my $icao (@apt) {
my $url = "http://www.atmos.albany.edu/cgi/sa-cgi?$icao";
my $content = get($url) or die "Error getting file: $!";
my ($pressure) = $content =~ /Pressure: ([\d.]+) millibars/;
push @pressure, $pressure;
}
return @pressure;
}
shadkeene
08-29-2007, 05:53 PM
Fishmonger,
The last, very concise code you sent, works! Thanks...looks real efficient.
If you have a chance sometime...what does the following actually do?
my ($pressure) = $content =~ /Pressure: ([\d.]+) millibars/;
I don't follow the pressure in parentheses followed by the $content. And I assume you're matching anything with Pressure: followed by a number followed by millibars. Do the parentheses of the $pressure match up with the digit and period in the parentheses...to reference the pressure numbers? Thanks,
Shad
FishMonger
08-29-2007, 06:08 PM
Read this perldoc, it should answer your questions about the regex.
c:\>perldoc perlre
shadkeene
08-29-2007, 06:24 PM
will do...thanks again
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.