Perl to send previous and current value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl to send previous and current value
# 1  
Old 05-20-2013
Perl to send previous and current value

For example, I have a file called number.txt.
Code:
x y
1 1
2 4
3 9
4 6
5 5
6 6
7 9
8 4
9 1
10 0
...

And I want to print out the value of x and y, if y%4==0 and the next value of y%4==0. Thus, the sample output is:
Code:
1 1 *because the previous x before 2 is 1
2 4 *because 4%4 == 0
7 9 *because the previous x before 8 is 7
8 4 *because 4%4 == 0
9 1 *because the previous x before 10 is 9
10 0 *because 0%4 == 0
...

Can someone do this in perl script? Thank you
# 2  
Old 05-20-2013
perl code

Code:
my $pre;
while(<DATA>){
	chomp;
	if($.==1){		
		$pre = $_;
		next;
	}
	else{
		my @arr = split(" ",$_);
		if($arr[1]%4==0){
			print $pre,"\n";
			print $_,"\n";
		}
		$pre = $_;
	}
}
__DATA__
1 1
2 4
3 9
4 6
5 5
6 6
7 9
8 4
9 1
10 0

This User Gave Thanks to summer_cherry For This Post:
# 3  
Old 05-21-2013
Quote:
Originally Posted by summer_cherry
Code:
my $pre;
while(<DATA>){
	chomp;
	if($.==1){		
		$pre = $_;
		next;
	}
	else{
		my @arr = split(" ",$_);
		if($arr[1]%4==0){
			print $pre,"\n";
			print $_,"\n";
		}
		$pre = $_;
	}
}
__DATA__
1 1
2 4
3 9
4 6
5 5
6 6
7 9
8 4
9 1
10 0

Dear Summer Cherry,

How if i want to put formula of (cur_val - 2 * pre_val + pre_pre_val) >= 0.25 into the perl? For example, I have a number.txt consists of
Code:
1 0.1
2 0.4
3 0.9
4 0.6
5 0.5
6 0.6
7 0.9
8 0.4
9 0.1
10 0.0

The expected process is
Code:
1 0.1 #by default it will print out the first two value
2 0.4 #by default it will print out the first two value
3 0.9 # ABS(0.9 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.2, 0.2 < 0.25, thus, will not printed out
4 0.6 # ABS(0.6 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.1, 0.1 < 0.25, thus, will not printed out
5 0.5 # ABS(0.5 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.2, 0.2 < 0.25, thus, will not printed out
6 0.6 # ABS(0.6 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.1, 0.1 < 0.25, thus, will not printed out
7 0.9 # ABS(0.9 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.2, 0.2 < 0.25, thus, will not printed out But because the x=8 is printed out, x=7 will be printed out as well.
8 0.4 # ABS(0.4 - 2 * 0.4 (y of 2)+ 0.1 (y of 1)) = 0.3, 0.3 >= 0.25, thus, will printed out
9 0.1 # ABS(0.1 - 2 * 0.4 (y of 8)+ 0.9 (y of 7)) = 0.2, 0.2 < 0.25, thus, will not printed out
10 0.0 # ABS(0.0 - 2 * 0.4 (y of 8)+ 0.9 (y of 7)) = 0.1, 0.1 < 0.25, thus, will not printed out

Thus, the output will be
Code:
1 0.1
2 0.4
7 0.9
8 0.4

So far, my perl script is:
Code:
#! /usr/bin/perl -w
#
# compare.pl
#
# Usage: compare.pl number.txt
#
#
use strict;

my $pre_value = 14091989;
my $pre_pre_value = 1;
while(<>){
  my( $ID, $cur_value) = split;
  if( abs($cur_value-2*$pre_value+$pre_pre_value) >= 0.25 ){
    print $_;
    $pre_pre_value = $pre_value;
    $pre_value = $cur_value;
  }
  else
  {
    $pre_value = $pre_value;
    $pre_pre_value = $pre_pre_value;
  }
}

I really need help here... Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to send data to previous program (pipe)?

Hi, Suppose I have a command:$ cmd1 | cmd2I need to send a message from cmd2 to cmd1 when I receive some a certain message from cmd1. How to do this? I think that I have to know cmd1's PID and then in cmd2 send a message to this PID. How? (24 Replies)
Discussion started by: JackK
24 Replies

2. UNIX for Advanced & Expert Users

Send current script to background

Hi scripters, I'm quite used to run commands in the background using & like in: $ myscript &But this is NOT what I'm trying to do today. What I'm trying to achieve is to run a script the normal way (without &), have my script do a little checkup and then jump to background. Something like:... (5 Replies)
Discussion started by: chebarbudo
5 Replies

3. Shell Programming and Scripting

How to compare previous and current item in for loop in bash?

Hey, I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals. At first I tried to store each log in and logo date in an array... (3 Replies)
Discussion started by: Mumu123
3 Replies

4. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

5. Shell Programming and Scripting

ksh comparing current and previous lines

Hi, I am currently trying to work out how to compare one line with the last line I have read in via ksh. I have a file which has sorted output from a previous sort command so all the lines are in order already and the file would look something like show below. Each line has a name and a time... (5 Replies)
Discussion started by: paulie
5 Replies

6. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

7. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

8. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

9. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

10. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies
Login or Register to Ask a Question