The following code checks all fields by identifying the value least like the others in the most recent few valid lines (invalid lines are those with bad values). Bad values are reported along with their line number.

#!/usr/bin/perl use warnings; use strict; my $window = 3; my @datCols = qw(temp1 press temp2 rain); my @allCols = (@datCols, qw(line code)); my %limits = (temp1 => 0.5, press => 10, temp2 => 0.5, rain => 0.2); my @history; while (<DATA>) { my %row; (my $code, @row{@datCols}) = split " "; $row{code} = $code; $row{line} = $.; if (@history > 2) { for my $col (@datCols) { my ($outlier, $avg) = outlier($col, @history, \%row); next if abs($outlier->{$col} - $avg) < $limits{$col} * ($wi +ndow - 1); print "$col == $outlier->{$col} (c.f. $avg) in line $outli +er->{line}\n"; $outlier->{$col} = undef; } } push @history, \%row; # Toss out any bad data rows @history = grep { my $row = $_; ! grep {!defined $row->{$_}} @datCols } @history; shift @history if @history > 3; } sub outlier { my $field = shift @_; my @list = sort {$a->{$field} <=> $b->{$field}} grep {defined $_->{$field +}} @_; my $sum; my $outlier; return if !@list; $sum += $_->{$field} for @list; $sum /= @list; if (abs($sum - $list[0]->{$field}) > abs($sum - $list[-1]->{$field +})) { $outlier = $list[0]; } else { $outlier = $list[-1]; } $sum = ($sum * @list - $outlier->{$field}) / (@list - 1); return $outlier, $sum; } __DATA__ A15 26.62 765 27.30 4.3 A11 26.63 763 27.28 4.2 A12 26.68 767 27.1 4.3 A16 26.64 768 27.0 4.2 A11 26.62 761 26.8 4.1 A15 26.62 765 26.6 4.3 A15 26.63 763 2.4 4.2 A16 26.68 767 26.1 4.3 A17 26.64 768 25.9 4.2 A18 26.62 761 25.7 4.1

Prints:

temp2 == 2.4 (c.f. 26.8) in line 7
True laziness is hard work

In reply to Re: comparing numbers from previous lines in a file? by GrandFather
in thread 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.