Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code: Example Perl program to read the CSV #29

Open
dtonhofer opened this issue Apr 26, 2018 · 0 comments
Open

Code: Example Perl program to read the CSV #29

dtonhofer opened this issue Apr 26, 2018 · 0 comments

Comments

@dtonhofer
Copy link

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
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant