LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-07-2013, 07:41 PM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
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;
  }
}

Last edited by eminempark; 01-07-2013 at 07:45 PM.
 
Old 01-07-2013, 08:13 PM   #2
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
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;

  }
}
 
  


Reply

Tags
difference, file, numbers, perlscript


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl to compare value differences eminempark Programming 1 01-06-2013 09:20 PM
bash: how to compare REAL numeric values? muebi Linux - General 3 02-28-2012 08:00 AM
[SOLVED] C Arguments to main - can't compare values madsovenielsen Programming 2 07-25-2011 07:17 AM
Compare Decimal Values rbautch Linux - General 10 04-22-2008 04:11 AM
How do I compare two values in separate files xmrkite Linux - Software 11 09-19-2007 11:36 PM


All times are GMT -5. The time now is 06:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration