On Jun 16, 4:08 pm, [EMAIL PROTECTED] wrote:

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

Okay, I'm going to answer your second question first.  In this
example:

our @foo;
while (do_stuff()){
  my @foo = get_values();
  #etc
}
print @foo;

The @foo that gets printed is the global @foo you declared at the
beginning.  It has nothing at all to do with the @foo you declared
inside the while loop.  The lexical @foo in the while loop "masks"
access to the global @foo you declared outside the while loop.[1]
That's why simply adding a 'our @SLPdataSFO;' statement to your
program would have no effect.



> 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

You are declaring your variable inside the while loop.  As soon as
this while loop ends, @SLPdataSFO goes out of scope.  It does not
exist anywhere else.  To fix this issue, *declare* the array before
the loop, but *assign* it within the loop:

my @SLPdataSFO;  #declaration
while (my $line = <FH>) {
  if ($line =~ m/^ Mean/) {
      @SLPdataSFO = split(/\s+/, $line);  #assigns values, but does
not redeclare
      #etc
   }
   #etc
}

Now a this point, the @SLPdataSFO you declared will still exist, and
will be populated with the last values put into it in your while loop.

There is no need to use 'our' here.  You simply need to fix the scope
of your lexical variable so that all pieces of code that want to use
the variable are within that scope.

Hope that helps,
Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to