in reply to comparing numbers from previous lines in a file?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: comparing numbers from previous lines in a file?
by ww (Archbishop) on Nov 22, 2013 at 13:13 UTC | |
by coding1227 (Novice) on Nov 22, 2013 at 19:53 UTC | |
by coding1227 (Novice) on Nov 22, 2013 at 20:26 UTC | |
by ww (Archbishop) on Nov 23, 2013 at 03:45 UTC | |
by coding1227 (Novice) on Nov 26, 2013 at 20:13 UTC |