-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-yesAnswer.pl
81 lines (71 loc) · 1.68 KB
/
filter-yesAnswer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl
# syntax:
# perl filter-yesAnswer.pl no-answer.ids.txt output.txt > output.yes-answer.txt
my $idfn=shift;
my $outputfn=shift;
open(my $idf,"<$idfn") or die "Couldn't open $idfn for reading:\n$!\n";
open(my $outputf,"<$outputfn") or die "Couldn't open $outputfn for reading:\n$!\n";
my $state=0;
my $buf="";
my $pass=1;
my $id="";
my $n=0;
while(<$idf>) {
chomp;
my $skip=$_;
if (length($id)>0) {
$pass=idlt($id,$skip);
#print "### Reading new skip: $id, $skip => $pass\n";
if ($pass<0) {
next;
}
}
while(<$outputf>) {
$n++;
#print "# '$_'\n";
if ($_ =~ /^\n$/) {
#print "## state transition: $state\n";
# state transition
if ($state == 0) {
$state++;
# then we just finished id. Check it and set pass bit
$id=$buf;
chomp($id);
$pass=idlt($id,$skip);
$buf = $buf . $_;
#print "### Current pass: $id, $skip => $pass\n";
if ($pass<0) {
# break to outer loop to fetch another skip id
last;
}
} elsif ($state == 4) {
$buf = $buf . $_;
$state=0;
if ($pass==1) { print $buf; }
$buf="";
} else {
$buf = $buf . $_;
$state++;
}
} else {
$buf = $buf . $_;
}
#($n<20000) or die "failed to parse";
}
}
if (length($buf)>0) { print $buf; }
while(<$outputf>) {
print $_;
}
close($idf);
close($outputf);
sub idlt{
my $id=shift;
my $skip=shift;
my @ids=split("q",substr($id,1,length($id)-1));
my @skips=split("q",substr($skip,1,length($skip)-1));
if ($ids[1]<$skips[1] && $ids[0] <= $skips[0]) { return 1; }
if ($ids[1]>=$skips[1] && $ids[0] < $skips[0]) { return 1; }
if ($ids[1]==$skips[1] && $ids[0] == $skips[0]) { return 0; }
return -1;
}