LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl to compare two values differences (http://www.linuxquestions.org/questions/programming-9/perl-to-compare-two-values-differences-4175444586/)

eminempark 01-07-2013 07:41 PM

Perl to compare two values differences
 
I have a data called "one.txt" and the contain is:
Code:

#ID Temperature
1 22.3125
2 22.3125
3 22.3125
4 22.25
5 22.25
6 22.25

Basically I need to filter out the data if the result=0 based on the following:
Code:

ID 1 |(22.3125-(2*0)+0)| = 22.3125 (PRINTED)
ID 2 |(22.3125-(2*22.3125(PRINTED ID1)+ 0)| = 22.3125 (PRINTED)
ID 3 |(22.3125-(2*22.3125(PRINTED ID2)+22.3125(PRINTED ID1)| = 0 (FILTERED OUT)
ID 4 |(22.25-(2*22.3125(PRINTED ID2)+22.3125(PRINTED ID1)| = 0.0625 (PRINTED)
ID 5 | 22.25-(2*22.25(PRINTED ID4)+22.3125(PRINTED ID2)| = 0.0625 (PRINTED)
ID 6 | 22.25-(2.22.25(PRINTED ID5)+22.25(PRINTED ID4)| = 0 (FILTERED OUT)

So, the result will be
Code:

1 22.3125
2 22.3125
4 22.25
5 22.25

And currently I have written this code but it does not working well
Code:

use strict;

my $printed_value1 = 0;
my $printed_value2 = 0;
while(<>){
  my( $ID, $current_value) = split;
  if( abs($current_value-(2*$printed_value1)+$printed_value2) != 0) {
    print $_;
    $printed_value1 = $current_value;
    $printed_value2 = $printed_value1;
  }
}


eminempark 01-07-2013 08:13 PM

It supposed to be like this:
Code:

use strict;

my $printed_value1 = 0;
my $printed_value2 = 0;
while(<>){
  my( $ID, $current_value) = split;
  if( abs($current_value-(2*$printed_value1)+$printed_value2) != 0) {
    print $_;
    $printed_value2 = $printed_value1;
    $printed_value1 = $current_value;

  }
}



All times are GMT -5. The time now is 01:22 PM.