laziness, impatience, and hubris PerlMonks

### Re: comparing numbers from previous lines in a file?

by Kenosis (Priest)
 on Nov 22, 2013 at 02:17 UTC ( #1063849=note: print w/replies, xml ) Need Help??

What if you kept a running average of col4 and compared the delta to that average minus the current value of col4?

```use strict;
use warnings;

my \$delta = .5;
my ( \$avg, \$sum, \$i );

while (<DATA>) {
\$i++;
my @cols = split;

if ( \$i == 1 ) {
\$avg = \$cols[3];
\$sum = \$avg;
next;
}

if ( abs( \$cols[3] - \$avg ) > \$delta ) {
print "Warning, Will Robinson: \$_";
\$i--;
next;
}

\$sum += \$cols[3];
\$avg = \$sum / \$i;
}

__DATA__
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

Output:

```Warning, Will Robinson: A16 26.68 767 2.29 4.3

Hope this helps!

Replies are listed 'Best First'.
Re^2: comparing numbers from previous lines in a file?
by GrandFather (Sage) on Nov 22, 2013 at 02:27 UTC

It's harder than that. Consider what happens if you have data that is trending down, but where the steps are well within the 0.5 criteria:

```use strict;
use warnings;

my \$delta = .5;
my (\$avg, \$sum, \$i);

while (<DATA>) {
\$i++;
my @cols = split;

if (\$i == 1) {
\$avg = \$cols[3];
\$sum = \$avg;
next;
}

if (abs(\$cols[3] - \$avg) > \$delta) {
print "Warning, Will Robinson: \$_";
\$i--;
next;
}

\$sum += \$cols[3];
\$avg = \$sum / \$i;
}

__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 26.3 4.2
A16 26.68 767 26.0 4.3
A17 26.64 768 25.7 4.2
A18 26.62 761 25.4 4.1

Prints:

```Warning, Will Robinson: A15 26.63 763 26.3 4.2
Warning, Will Robinson: A16 26.68 767 26.0 4.3
Warning, Will Robinson: A17 26.64 768 25.7 4.2
Warning, Will Robinson: A18 26.62 761 25.4 4.1
True laziness is hard work

Yes, good point. Had wondered whether I'd oversimplified the issue...

Edit: On second thought, it might not be "harder than that". The OP did say, "all of which are approx. 27.3 degrees Celsius", so it depends solely upon the OP's data set. There may never be an instance of such a downtrending of values--especially the way the OP characterized the data.

Create A New User
Node Status?
node history
Node Type: note [id://1063849]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-05-21 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (151 votes). Check out past polls.

Notices?