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-06-2013, 09:13 PM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
Perl to compare value differences


For example, I have a data in "one.txt" which is
Code:
# ID Temperature
1 25.0
2 25.0
3 25.1
4 25.6
5 25.7
6 25.9
And I want to filter out the temperature which has difference less than 0.2 with previous REGISTERED value. The output should be
Code:
# ID Temperature
1 25.0
4 25.6
6 25.9
The mechanism works like this
ID 1, diff |25.0 - 0| = 25.0 (ID 1 register)
ID 2, diff |25.0 - 25.0 (TEMP. FROM ID 1)| = 0 (ID 2 filter out)
ID 3, diff |25.1 - 25.0 (TEMP. FROM ID 1)| = 0.1 (ID 3 filter out)
ID 4, diff |25.6 - 25.0 (TEMP. FROM ID 1)| = 0.6 (ID 4 register)
ID 5, diff |25.7 - 25.6 (TEMP. FROM ID 4)| = 0.1 (ID 5 filter out)
ID 6, diff |25.9 - 25.6 (TEMP. FROM ID 4)| = 0.3 (ID 6 register)
Any idea?

Last edited by eminempark; 01-06-2013 at 09:14 PM.
 
Old 01-06-2013, 10:20 PM   #2
binary_pearl
Member
 
Registered: Jul 2007
Location: Chicago Illinois
Distribution: SLES 10 SP2/SP3, SLES 11 SP1, OpenSUSE, Sabayon, Gentoo, Fedora 14, RHEL 3/4/5/6
Posts: 98
Blog Entries: 1

Rep: Reputation: 23
Not sure how familiar you are with perl, but this framework below may help. What it's doing is to open the file one.txt, and then read the contents into an array (@records). If then loops through each line, using a regular expression to break apart the id, the integear part of the temperature and the decimal part of the temperature. From there you can use those variables ($id, $int, $decimal) for further math. Hope this helps:

Code:
#!/usr/bin/perl

use strict;

my @records = ();
my $record = "";

my $id = "";
my $int = "";
my $decimal = "";

open (INPUTFILE, "one.txt");

@records = <INPUTFILE>;

foreach $record (@records) {
        chomp $record;

        if ($record =~ m/([0-9].*)( )([0-9].*)(.)([0-9].*)/) {
                $id = $1;
                $int = $3;
                $decimal = $5;

                #... do stuff
        }
}

close (INPUTFILE);
--Shaun
 
  


Reply

Tags
difference, perlscript, text processing, values


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
Compare file extension from two different txt file and find the differences. Neal000 Programming 6 08-28-2012 03:03 PM
Tool like diff to compare two files & make graphical output highlighting differences? kmkocot Linux - Newbie 1 08-02-2012 10:26 PM
compare two files in perl kariagekun Linux - Newbie 5 10-27-2009 08:38 PM
Where exactly Perl & C/C++ are getting different and unique compare to each other..? deepak_p86 Programming 4 07-15-2009 12:52 AM
Perl List::Compare printing PB0711 Programming 2 08-10-2006 01:59 PM


All times are GMT -5. The time now is 11:29 PM.

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