Re: Problems referencing variable value in another loop



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;
.



Relevant Pages

  • Re: beginners question
    ... Your script works fine. ... Using the for loop, that line has already been read so you do not need to ... line to extract the words. ... write True or False to your output file ...
    (comp.lang.python)
  • Re: Mixing Shell variables and AWK variables
    ... I am writing a script that will extract a ... > particular digit from a loop. ...
    (comp.unix.shell)
  • ReadDirectoryChangesW seem to looses events?
    ... a directory I use functions "ReadDirectoryChangesW" ... asynchronously with function "GetQueuedCompletionStatus" ... (extract of my code copied below.) ... For some reason, my loop thread works ...
    (microsoft.public.win32.programmer.kernel)
  • Problems referencing variable value in another loop
    ... like to extract data from two files, ... the first while loop, the script doesn't recognize it. ... into individual elements between whitespace ...
    (perl.beginners)
  • Re: Creating many letters(mail merge) from a single template in C#
    ... I understand that you and users decide to use the interface to insert ... In this case, as a developer, you basically need to loop through all the fields ... generates in order to figure out how best to extract the information you need ... This reply is posted in the Newsgroup; please post any follow question or reply ...
    (microsoft.public.office.developer.automation)