-
Notifications
You must be signed in to change notification settings - Fork 0
/
callsign-lookup.pl
95 lines (65 loc) · 1.78 KB
/
callsign-lookup.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
#use strict;
#use warnings;
#use Data::Dumper;
sub string_compare($$) {
my $s1 = shift;
my $s2 = shift;
my $i = 0;
while (1) {
if ( substr($s1, $i, 1) && substr($s2, $i, 1) ) {
if (substr($s1, $i, 1) eq substr($s2, $i, 1)) {
$i++;
next;
}
}
last;
}
return $i;
}
my $call = uc shift @ARGV;
my %db;
while (<>) {
my ($prefix, $country) = split(/\t/, $_);
#print "line: $_";
chomp($country);
if (index($prefix, '-') != -1) {
my ($from, $to) = split('-', $prefix);
#print "from: " . $from . " to: " . $to . "\n";
$prefix = $from;
while (1) {
#print "Add prefix: " . $prefix . " country: " . $country . "\n";
$db{$prefix} = $country;
last if ($prefix eq $to);
if (substr($prefix, -1, 1) eq 'Z') {
substr($prefix, -1, 1) = 'A';
substr($prefix, -2, 1) = chr(ord(substr($prefix, -2, 1))+1);
} else {
substr($prefix, -1, 1) = chr(ord(substr($prefix, -1, 1))+1);
}
}
} else {
#print "Add prefix: " . $prefix . " country: " . $country . "\n";
$db{$prefix} = $country;
}
}
#print Dumper(\%db);
my $longest_match = 0;
foreach my $prefix (keys %db) {
if (string_compare($prefix, $call) > $longest_match) {
$longest_match = string_compare($prefix, $call);
if ($longest_match == 3) {
print;
}
}
}
if ($longest_match == 0) {
print "Not found.\n";
exit(1);
}
#print "longest match: $longest_match\n";
foreach my $prefix (keys %db) {
if (string_compare($prefix, $call) == $longest_match) {
print "Prefix: $prefix Country: $db{$prefix}\n";
}
}