Your main problem is that you are declaring variables with "my" inside the while loop, but then you are trying to use those variables outside the loop, where they become undefined/undeclared. The script, as posted, will generate syntax errors on the final "print" statement, because all the variables being printed have gone "out of scope" (at that point, program execution has exited the block in which they were defined -- i.e. the while loop).
(You also have a syntax error from repeating "my" twice on the line where you assign values to $LAT and $LON, as well as mismatched quotes in the last line of the while loop.)
You need to declare the variables that will be printed before you go into the loop, then assign values to them as appropriate within the loop, then print them when the loop is done. Something like this:
Now, that version will do basically what the OP code would do (if it were runnable), but I'm not sure this is what you really want to do. It will simply print out the 6 fields of interest that come from the last line of data in the file, together with the lat/lon value from the file's header.#!/usr/bin/perl -w #... (documentation could be a little better... consider using pod) use strict; use warnings; die "Usage: $0 textfile\n" unless ( @ARGV == 1 and -f $ARGV[0] ); my $txtfile = shift; open(IN, $txtfile) or die "open failed on $txtfile: $!"; my ( $LAT, $LON, $PRES, $HGHT, $TEMP, $DEWP, $DIR, $SPD ); while (<IN>) { if ( /<pre>/ ) { # cue to grab next line for lat/lon $_ = <IN>; # use a regex to match and capture floating-pt n +umbers: ( $lat, $lon ) = ( /\s+ (-?\d+\.\d+) \s+ (-?\d+\.\d+) \s+/x; } elsif ( /^\s*(?:SFC|\d+)\s+\d+/ ) { # line of data my @flds = split; ( $PRES, $HGHT, $TEMP, $DEWP, $DIR, $SPD ) = @flds[1..4,8,9]; } } open( OUT, ">", "$txtfile.reduced" ) or die "open failed on $txtfile.r +educed: $!"; print OUT "$LAT, $LON, $PRES, $HGHT, $TEMP, $DEWP, $DIR, $SPD\n"; close OUT;
If that's what you want, then your done. But I would have expected something more. And then again, it doesn't seem as though you are showing us the whole data file -- there is no </pre> tag to be found in the sample...
In reply to Re: PARSER help
by graff
in thread PARSER help
by MKevin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |