Skip to content

Instantly share code, notes, and snippets.

@wolfjohns
Last active March 29, 2022 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfjohns/39ae8574551be84728c6305a78adfe09 to your computer and use it in GitHub Desktop.
Save wolfjohns/39ae8574551be84728c6305a78adfe09 to your computer and use it in GitHub Desktop.
geekuni Topic 7 q11
#!/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));
#!/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";
}
@andrewsolomon
Copy link

A) Arguments should being passed in as a hash:

sub get_name_matching_airports {
    my %args = (
        airports        => undef,
        matching_string => undef,
        word            => undef,
        @_
    );
    ...
}

then it's called like

  $rah_airports_found = get_name_matching_airports(
    airports        => $rah_airports,
    matching_string => $matching,
    word            => $word,
  );

B) Line 27 in your code

    my $fields = parse_airports($filename);

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment