Hi everyone I am trying to learn more about Perl and work with some weather data. I have an ascii file (shown below) that has 10 lines with different columns. What I would like is have Perl find an "anomalous" value by comparing a field with the values from the last 3 lines (for the same field, of course). For instance, if we look at the data below:

A15 26.62 765 27.30 4.3

A11 26.63 763 27.28 4.2

A12 26.68 767 27.29 4.3

A16 26.64 768 27.30 4.2

A11 26.62 761 27.31 4.1

A15 26.62 765 27.30 4.3

A15 26.63 763 27.28 4.2

A16 26.68 767 2.29 4.3

A17 26.64 768 27.30 4.2

A18 26.62 761 27.31 4.1

We see here that on the 8th line, the value in column 4 is "off" with respect to the previous lines (all of which are approx. 27.3 degrees Celsius). Thus, how can I do to compare the values for a line in column 4 with the values from the previous 3 lines in order to detect these "bad" data values? Assume that any value that dirfts +/- than 0.5deg C is "bad data" I am not sure how to proceed. Do I need to load the data into an array? Do I need to read the file line by line? Or maybe something else? Any help, suggestions, and especially examples would be really helpful. Thanks in advance Sorry... I forgot to mention that this is what I have in my script so far:
my $file = "$WORK_DIR/tmpdata.asc"; open my $info, $file or die "Could not open $file: $!"; my %line; local @ARGV = ($file); while ( <$info> ) { my $x1 = $_; my @cols = split(" ", $x1); print "@cols\n"; } close $info;

In reply to comparing numbers from previous lines in a file? by coding1227

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.