I've got a set of data in a text file which looks like the following:
Data1 Data2 Data3
0 0 23
0 1 0
0 2 3
...
1 0 45
1 1 23
1 2 0
The number of rows is totally variable
There are 2 halves of data separated by Data1 and the change is reflected when it changes from 0 to 1 (i.e. Data1 can only be 0 or 1)
Data2 does not always start from 0, but usually does and increments are not necessarily by one or sequential. The Data2 numbers are exactly a mirror of each other from the two halves.
What I want to do is to go thru each line and find the Data2 value in the top half, and see if the Data3 value is > 0. If it is, then find the corresponding Data2 value in the bottom half and see if it greater than 0. If they are both greater than zero, then output the results to a text file
So in our example above, the result match would be
0 0 23
1 0 45
So far, I have the following:
This currently finds which values have Data3 > 0. I can't work out the logic to search thru Data2 for match values.
Any ideas would be appreciated.
Data1 Data2 Data3
0 0 23
0 1 0
0 2 3
...
1 0 45
1 1 23
1 2 0
The number of rows is totally variable
There are 2 halves of data separated by Data1 and the change is reflected when it changes from 0 to 1 (i.e. Data1 can only be 0 or 1)
Data2 does not always start from 0, but usually does and increments are not necessarily by one or sequential. The Data2 numbers are exactly a mirror of each other from the two halves.
What I want to do is to go thru each line and find the Data2 value in the top half, and see if the Data3 value is > 0. If it is, then find the corresponding Data2 value in the bottom half and see if it greater than 0. If they are both greater than zero, then output the results to a text file
So in our example above, the result match would be
0 0 23
1 0 45
So far, I have the following:
Code:
use strict;
use warnings;
LINE:
while (<DATA>) {
chomp;
my $line = $_;
my ($num) = ($line =~ m{^\d+\s+\d+\s+(\d+)})
? $1
: do {warn "Line does not match pattern: $line"; next LINE};
if ($num > 0) {
print "$line\n";
}
}
__DATA__
Data1 Data2 Data3
0 0 23
0 1 0
0 2 3
1 0 45
1 1 23
1 2 0
This currently finds which values have Data3 > 0. I can't work out the logic to search thru Data2 for match values.
Any ideas would be appreciated.