You have a few problems, apart from your main question. It's good that you have use strict but you need to understand how scoping of variables really works.

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:

#!/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;
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.