Last active
March 29, 2022 16:42
-
-
Save wolfjohns/39ae8574551be84728c6305a78adfe09 to your computer and use it in GitHub Desktop.
geekuni Topic 7 q11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Text::CSV qw(csv); | |
use Data::Dumper qw(Dumper); | |
my $filename = 'iata_airports.csv'; | |
my $matching = $ARGV[0]; | |
my @matches; | |
my $airports = csv( | |
in => $filename, | |
headers => 'auto'); | |
#print Dumper $airports; | |
#return $airports; | |
for my $i (@{ $airports }) { | |
print "name: $i->{name}\n"; | |
for my $key (keys %$i) { | |
#print "%$i\n"; | |
print "$key=$i->{$key},"; | |
} | |
print "\n"; | |
} | |
sub get_name_matching_airports { | |
# my $fields = parse_airports($airports); | |
my $match = shift; | |
for my $x (@{ $airports }) { | |
if ($x->{name} =~ /$match/i) { | |
# print "$x\n"; | |
push @matches, $x; | |
} | |
} | |
return @matches; | |
# print Dumper(@matches); | |
} | |
print Dumper(get_name_matching_airports($matching)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Data::Dump 'pp'; | |
use feature 'say'; | |
use Getopt::Long; | |
use Text::CSV qw(csv); | |
#### | |
#FILL ME IN!! | |
sub parse_airports { | |
my $filename = shift; | |
open(my $data, '<:encoding(utf8)', $filename) or die "Could not open $filename' $!\n"; | |
my $airports = csv( | |
in => $data, | |
headers => 'auto'); | |
return $airports; | |
} | |
sub get_name_matching_airports { | |
my $fields = parse_airports($filename); | |
my ($match, $str) = @_; | |
for my $x (@{ $fields }) { | |
if ($match =~ /$x->{name}/i) { | |
push @matches, $x; | |
} | |
} | |
return @matches; | |
} | |
my $filename = '/home/student/perl-basic/topic-07/iata_airports.csv'; | |
my $number = 1; | |
my $matching; | |
my $latitude; | |
my $longitude; | |
my $word; | |
my @matches; | |
GetOptions ( | |
'filename:s' => \$filename, | |
'number:i' => \$number, | |
'matching:s' => \$matching, | |
'latitude:f' => \$latitude, | |
'longitude:f' => \$longitude, | |
'word:s' => \$word, | |
) or die "Invalid options passed $0\n"; | |
#### | |
my $rah_airports = parse_airports($filename); | |
my $rah_airports_found = []; | |
get_name_matching_airports($matching, $words); | |
if ($matching) { | |
say "Up to $number airports matching $matching in $filename:"; | |
} | |
elsif ($latitude && $longitude) { | |
say "Up to $number airports near [$latitude, $longitude] in $filename:" | |
} | |
else { | |
say "Must have at least --matching, or --latitude and --longitude as arguments"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A) Arguments should being passed in as a hash:
then it's called like
B) Line 27 in your code
is ignoring the arguments passed in and reading from a global variable. This will make the code difficult to turn into a module (and it will also confuse tutor-bot which is passing in its own list of airports).
C) Note that the match should be case insensitive