You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This can be included in the project's code base, if interest.
#!/usr/bin/perl
use warnings;
use strict;
use Text::CSV; # http://search.cpan.org/~ishigaki/Text-CSV-1.95/lib/Text/CSV.pm
use Carp; # http://search.cpan.org/~rjbs/Carp-1.38/lib/Carp.pm
my $airportsFile = 'airport-codes.csv';
my $onlyIcao = 1;
my $quiet = 1;
# >>>>
my $airports = readAirports($airportsFile,$onlyIcao,$quiet);
# <<<<
if (! defined $airports) {
print STDERR "No result obtained!\n";
}
else {
for my $key (sort(keys(%$airports))) {
my $val = $$airports{$key}; # this is a hashref
print $$val{ident},"\n";
for my $subkey (sort(keys(%$val))) {
if ($subkey ne 'ident') {
print sprintf("%15s = %s\n",$subkey,$$val{$subkey})
}
}
}
}
# ===
# Read the CSV file of airport records.
# Returns a hashref if all went well, or undef if not!
# ===
sub readAirports {
my($airportsFile,$onlyIcao,$quiet) = @_;
my @rows;
my $csv = Text::CSV->new ( { binary => 1 } ) or croak "Cannot use Text::CSV: " . Text::CSV->error_diag();
my $fh;
unless (open($fh, "<:encoding(utf8)", $airportsFile)) {
print STDERR "Could not open file '$airportsFile': $!\n";
return undef
}
print STDERR "Reading airport information from file '$airportsFile'...\n";
my $count = -1;
my $headers;
my $airports = {};
while (my $row = $csv->getline($fh)) {
$count++;
if ($count == 0) {
# first entry consists of headers
$headers = $row;
}
else {
# transform record into a map of key -> value pairs
my $airport = {};
for my $header (@$headers) {
$$airport{$header} = $$row[0];
shift @$row;
}
my $ident = $$airport{ident}; # "ident" may or may not be ICAO code
if ($onlyIcao && (!($ident =~ /^[A-Z]{4}$/) || $ident eq 'ZZZZ')) {
print STDERR "Ident '$ident' is definitely not an ICAO code -- skipping\n" unless $quiet;
}
else {
# retain
croak "Clash on airport identifier '$ident'" if exists $$airports{$ident};
my $name = $$airport{name};
$$airports{$ident} = $airport;
}
if ($count % 5000 == 0) {
print STDERR "$count airports read so far...\n"
}
}
}
$csv->eof or $csv->error_diag();
close $fh or carp "Could not close file '$airportsFile: $!";
print STDERR "$count airports read in total, " . scalar(keys %$airports) . " retained!\n";
return $airports
}
The text was updated successfully, but these errors were encountered:
This can be included in the project's code base, if interest.
The text was updated successfully, but these errors were encountered: