CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl to compare value and to print certain line

 



eminempark
Novice

May 29, 2013, 6:44 PM

Post #1 of 6 (307 views)
Perl to compare value and to print certain line Can't Post

For example I have a number.txt
The data is

Code
1 0.058  
2 0.029
3 0.053
4 0.047
5 0.012
6 0.064
7 0.088
8 0.220
9 0.083

and the threshold is 0.072.

And I would like only to print out the ID and value if

Code
(1) it is the $cur_val = 1  
(2)$cur_val-2*last_val+pre_print_val exceeds threshold. Mathematically is:
$cur_val-2*last_val+pre_print_val > th
(3) it is the previous and second previous value of the printed value.
For example: if I printed the value of 8, i need to print the value of 7 and 6.


The idea of the last printed value is that:

Code
1. if the last printed value == last value, then last printed value == 2nd last printed value.  
2. if the last printed value != last value, then last printed value == last printed value.


So, the process are:

Code
	 
1 0.058 (process: $ID = 1, thus print)
2 0.029 (process: abs(0.029-2*0.058+0)=0.087, thus print)
3 0.053 (process: abs(0.053-2*0.029+0.058)=0.053, thus not print)
4 0.047 (process: abs(0.047-2*0.053+0.029)=0.030, thus not print)
5 0.012 (process: abs(0.012-2*0.047+0.029)=0.053, thus not print)
6 0.064 (process: abs(0.064-2*0.012+0.029)=0.069, thus not print. But because 8 is printed out, 6 will be printed out as well)
7 0.088 (process: abs(0.088-2*0.064+0.029)=0.011, thus not print. But because 8 is printed out, 7 will be printed out as well)
8 0.220 (process: abs(0.220-2*0.088+0.029)=0.073, thus print)
9 0.083 (process: abs(0.083-2*0.220+0.029)=0.328, thus print)


Thus, the output will be:

Code
1 0.058  
2 0.029
6 0.064
7 0.088
8 0.220
9 0.083


Any help is appreciated. I upload the excel file if what I write is not clear.Laugh


eminempark
Novice

May 30, 2013, 12:33 AM

Post #2 of 6 (301 views)
Re: [eminempark] Perl to compare value and to print certain line [In reply to] Can't Post

I have did some code

Code
#! /usr/bin/perl -w 
#
# compare.pl
#
# Usage: compare.pl burst1.txt
#
#
use strict;

my $pre_x = 0;
my $pre_pri_x = 0;
my $pre_pre_pri_x = 0;

while(<>){
my( $ID, $x) = split;
if ($ID == 1)
{
printf ("%d %.3f %.3f %.3f\n", $ID, $x, $pre_x, $pre_pri_x);
$pre_pre_pri_x = $pre_pri_x;
$pre_pri_x = $pre_x;
$pre_x = $x;
}
elsif ($ID != 1)
{
my $vector = abs($x-2*$pre_x+$pre_pri_x);
if ($vector >= 0.072)
{
if ($pre_pri_x == $pre_x)
{

printf ("%d %.3f %.3f %.3f\n", $ID, $x, $pre_x, $pre_pri_x);
$pre_pri_x = $pre_pre_pri_x;
$pre_x=$x;
}
else
{
printf ("%d %.3f %.3f %.3f\n", $ID, $x, $pre_x, $pre_pri_x);
$pre_pri_x = $pre_pri_x;
$pre_x=$x;
}
}
else
{
$pre_pri_x = $pre_pri_x;
$pre_x = $x;
}
}
}


But the pre_pre_pri_x has give me wrong value.
It give me output

Code
1 0.058 0.000 0.000 
2 0.029 0.058 0.000
5 0.012 0.047 0.000
9 0.083 0.220 0.000



(This post was edited by eminempark on May 30, 2013, 12:39 AM)


Laurent_R
Enthusiast / Moderator

May 30, 2013, 8:50 AM

Post #3 of 6 (288 views)
Re: [eminempark] Perl to compare value and to print certain line [In reply to] Can't Post

Hi,

I posted something yesterday, answering a similar question from you on another post, but no longer find it today. Hmm, strange...

Anyway, below is what I wrote yesterday

____

Hi,

I had a little bit of trouble understanding how you update the pre-pre-value, I think I have it right now. At least, it produces the same output as your example.

As I understand thez rules have changed quite a bit compared to the previous version:
- pre-value is always the value from the previous line
- the other one seems to be the previous match or the match before, depending on whether anothe rline has been processed.


Code
use strict;   
my $pre_value = 0;
my $pre_match = 0;
my $pre_pre_match = 0;
my ($antepenultimate, $previous_line) = ("", "");
while(<DATA>){
my( $ID, $cur_value) = split;
if ($ID == 1) {
print;
$pre_value = $cur_value;
$pre_match = $cur_value;
next;
}
if( abs($cur_value - 2 * $pre_value + $pre_pre_match) >= 0.072 ){
if ($previous_line) {
print $antepenultimate if defined $antepenultimate;
print $previous_line;
# $pre_pre_value = $pre_value; # this line is useless but keeping it conveys the logic better
# $pre_value = (split / /, $previous_line)[1];
}
print $_;
$pre_match = $cur_value;
$previous_line = "";
$antepenultimate = "";
} else {
$antepenultimate = $previous_line;
$previous_line = $_;
}
$pre_value = $cur_value;
$pre_pre_match = $pre_match;
}
__DATA__
1 0.058
2 0.029
3 0.053
4 0.047
5 0.012
6 0.064
7 0.088
8 0.220
9 0.083


The following is the output:


Code
$perl PREVALUE.PL 
1 0.058
2 0.029
6 0.064
7 0.088
8 0.220
9 0.083



eminempark
Novice

May 30, 2013, 8:33 PM

Post #4 of 6 (282 views)
Re: [Laurent_R] Perl to compare value and to print certain line [In reply to] Can't Post

Hi, Laurent, first of all, thank you very much.

I would like to ask about the pre_match and pre_pre_match. Are the pre_match and pre_pre_match is the previous printed value and the previous previous printed value?


Laurent_R
Enthusiast / Moderator

May 31, 2013, 6:07 AM

Post #5 of 6 (269 views)
Re: [eminempark] Perl to compare value and to print certain line [In reply to] Can't Post

Yes, but only if it was printed because the calculated result satisfied the condition (not it is is printed because another later line is printed. This is the way I understood your example.


eminempark
Novice

Jun 2, 2013, 10:10 AM

Post #6 of 6 (254 views)
Re: [Laurent_R] Perl to compare value and to print certain line [In reply to] Can't Post

Dear Laurent_R, how if:

I would like only to print out the ID and value if

Code
(1) it is the $cur_val = 1   
(2)$cur_val-2*last_val+pre_pri_val exceeds threshold. Mathematically is:
$cur_val-2*last_val+pre_pri_val > th
(3) it is the previous and second previous value of the printed value.
For example: if I printed the value of 8, i need to print the value of 7 and 6.

The idea of the last printed value is that:

Code
	 
1. if the $last_ID - $ID == 1, then $pre_pri_val == last value.
2. if the $last_ID - $ID != 1, then $pre_pri_val == $pre_pri_val.

So, the process are:

Code
	  
1 0.058 (process: $ID = 1, thus print)
2 0.029 (process: abs(0.029-2*0.058+0)=0.087, thus print)
3 0.053 (process: abs(0.053-2*0.029+0.058)=0.053, thus not print)
4 0.047 (process: abs(0.047-2*0.053+0.058)=0.001, thus not print. But be cause $ID = 6 is printed, $ID = 4 will be printed as well.)
5 0.012 (process: abs(0.012-2*0.047+0.058)=0.024, thus not print. But because $ID = 6 is printed, $ID = 5 will be printed as well.)
6 0.064 (process: abs(0.064-2*0.012+0.058)=0.098, thus print.)
7 0.088 (process: abs(0.088-2*0.064+0.012)=0.028, thus not print. But because $ID = 9 is printed, $ID = 7 will be printed as well.)
8 0.220 (process: abs(0.220-2*0.088+0.012)=0.056, thus not print. But because $ID = 9 is printed, $ID = 8 will be printed as well.)
9 0.083 (process: abs(0.083-2*0.220+0.012)=0.345, thus print)

Thus, the output will be:

Code
	 
1 0.058
2 0.029
4 0.047
5 0.012
6 0.064
7 0.088
8 0.220
9 0.083


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives