Perl to send previous and current value

For example, I have a file called number.txt.

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:

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

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
1 Like

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

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

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

1 0.1
2 0.4
7 0.9
8 0.4

So far, my perl script is:

#! /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