-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetClosestRows.pl
More file actions
executable file
·57 lines (42 loc) · 1.09 KB
/
getClosestRows.pl
File metadata and controls
executable file
·57 lines (42 loc) · 1.09 KB
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
#!/usr/bin/perl -w
if((@ARGV) != 3) {
print "Get the rows with values in the specified field that are closest to the specified point\n";
print "\nUsage: ~ <input_file>( <fld_index> <number>\n";
print " fld_index\ta numeric field of interest\n";
print " number\tthe point of interest\n\n";
exit(1);
}
use Flat;
use Util;
my $cmdLine = Util::getCmdLine();
# read data from the file
my($in) = Flat->new1(shift @ARGV);
my $fld = $in->getFieldIndex(shift @ARGV);
my $num = shift @ARGV;
print "# $cmdLine > <this_file>\n";
my $cmt = $in->getComments();
if($cmt) {
print "$cmt";
}
print join("\t", $in->getFieldNames()), "\n";
my @minRows = ();
my $row = $in->readNextRow();
my $minDist;
if($row) {
$minDist = abs($num - $row->[$fld]);
@minRows = ($row);
}
while($row = $in->readNextRow()) {
$dist = abs($num - $row->[$fld]);
if($dist < $minDist) {
@minRows = ($row);
$minDist = $dist;
}
elsif($dist == $minDist) {
push @minRows, $row;
}
# else $dist > $minDist, skip
}
foreach $r (@minRows) {
print join("\t", @{$r}), "\n";
}