Re: Problems referencing variable value in another loop
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Sun, 17 Jun 2007 18:59:17 +0100
shadkeene@xxxxxxxxxxx wrote:
Hi,
I was able to parse different data in the same loop...however, now I'd
like to extract data from two files, using two while loops after
opening each .txt file...then do arithmetic with the data I
extracted...all in the same script. The problem is that when I
reference a variable in the second while loop that I initialized in
the first while loop, the script doesn't recognize it. To be more
clear, there is a number value for $SLPdataSFO[6] that I extracted in
the first while loop, and I'd like to subtract that value from
$SLPdataSAC[6], which I extracted in the second while loop. I tried to
make it a global variable at the beginning by using "our @SLPdataSFO =
();" to make it a global variable, but no luck there. Any ideas?
Thanks for your help....the error I receive is as follows with the
script after that.
Shad
Global symbol "@SLPdataSFO" requires explicit package name at c:\perl-
scripts\BarkerRegParser.cgi line 54.
Execution of c:\perl-scripts\BarkerRegParser.cgi aborted due to
compilation errors.
#!/perl/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
use strict;
print header;
print start_html("Gradients");
open(FH, "C:/perl-scripts/Data/BarkerSFOtest.txt") #open barker SFO
extrapolated NAM data .txt file
or &dienice("couldn't open output file: $!");
print "<h2>PW</h2>";
while (my $line = <FH>) { #reads through each line of model data
if ($line =~ m/^ Mean/) { #finds line with sealevel pressure
print "$line<br>";
my $SFOdataSLP = $line; #places $line into new string called $data SLP
my @SLPdataSFO = split(/\s+/, $SFOdataSLP); #splits $data SLP string
into individual elements between whitespace
print "$SLPdataSFO[6]<br>";
print "$SLPdataSFO[4]<br>";
my $SLPchgSFO = $SLPdataSFO[6] -= $SLPdataSFO[4]; #subtracts 12hr SLP
from 0hr SLP
print "$SLPchgSFO<br>";
my $roundf = sprintf("%1.1f", $SLPchgSFO);
print "$roundf<br>";
}
elsif ($line =~ m/^ Precip/) { #if line isn't Precip Water line, then
will skip to next line
print "$line<br>";
my $dataPW = $line;
my @PWdata = split(/\s+/, $dataPW);
print "$PWdata[6]<br>";
}
}
close (FH);
open(FH, "C:/perl-scripts/Data/BarkerSACtest.txt") #open barker SFO
extrapolated NAM data .txt file
or &dienice("couldn't open output file: $!");
while (my $line = <FH>) { #reads through each line of model data
if ($line =~ m/^ Mean/) { #finds line with mean sealevel pressure
print "$line<br>";
my $SACdataSLP = $line; #places $line into new string called
$SACdataSLP
my @SLPdataSAC = split(/\s+/, $SACdataSLP); #splits $SACdataSLP string
into individual elements between whitespace
my $SACSFOgrad12 = $SLPdataSFO[6] -= $SLPdataSAC[6]; #subtracts SFO
from SAC 12-hr fcst sealevel pressure
print "$SACSFOgrad12";
}
}
close (FH);
print end_html;
sub dienice {
my($errmsg) = @_;
print "<h2>Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;
exit;
}
I assume your files contain just one example of each of the data lines that you're
interested in? In which case you're doing far more work inside the loop than is
appropriate. Consider something like the code below, which uses the while loops
simply to search for the data required and then processes it separately. There are
better ways to do this, but not without going a long way from your original code.
HTH,
Rob
my ($SFOdataSLP, $dataPW);
open FH, "C:/perl-scripts/Data/BarkerSFOtest.txt" #open barker SFO extrapolated NAM data .txt file
or dienice("couldn't open output file: $!");
while (<FH>) {
$SFOdataSLP = $_ if /^ Mean/;
$dataPW = $_ if /^ Precip/;
}
close FH;
print "$SFOdataSLP<br>";
my @SLPdataSFO = split /\s+/, $SFOdataSLP;
print "$SLPdataSFO[6]<br>";
print "$SLPdataSFO[4]<br>";
my $SLPchgSFO = $SLPdataSFO[6] - $SLPdataSFO[4]; #subtracts 12hr SLP from 0hr SLP
print "$SLPchgSFO<br>";
my $roundf = sprintf("%1.1f", $SLPchgSFO);
print "$roundf<br>";
print "$dataPW<br>";
my @PWdata = split /\s+/, $dataPW;
print "$PWdata[6]<br>";
my $SACdataSLP;
open FH, "C:/perl-scripts/Data/BarkerSACtest.txt" #open barker SFO extrapolated NAM data .txt file
or dienice("couldn't open output file: $!");
while (<FH>) {
$SACdataSLP = $_ if /^ Mean/;
}
close FH;
print "$SACdataSLP<br>";
my @SLPdataSAC = split /\s+/, $SACdataSLP;
my $SACSFOgrad12 = $SLPdataSFO[6] -= $SLPdataSAC[6]; #subtracts SFO from SAC 12-hr fcst sealevel pressure
print "$SACSFOgrad12";
print end_html;
.
- References:
- Problems referencing variable value in another loop
- From: shadkeene
- Problems referencing variable value in another loop
- Prev by Date: Re: character encoding & regex
- Next by Date: php to perl translation
- Previous by thread: Re: Problems referencing variable value in another loop
- Next by thread: Unable to reference variable from one loop to another in same script
- Index(es):
Relevant Pages
|