GetOption - Perl - 引用

时间:2016-02-10 11:14:35

标签: perl hash reference

所以当我试图建立一个简单的机场搜索脚本时,我偶然发现了一个问题。在Perl。

my $filename = '/home/student/perl-basic/topic-07/iata_airports.csv';
my $number = '1';
my $matching;
my $latitude;
my $longitude;
my $word = 'false';

GetOptions (
        "filename=s"    =>      \$filename,
        "number=i"      =>      \$number,
        "matching=s"    =>      \$matching,
        "latitude=f"    =>      \$latitude,
        "longitude=f"   =>      \$longitude,
        "word=s"        =>      \$word
);

sub parse_airports {

        my $file = shift;

        my $csv = Text::CSV->new( { binary => 1, eol => $/ } );

        open ( my $fh, "<", $file ), or die "Error opening input file: $!";

                my $ra_colnames = $csv->getline ( $fh );
                        $csv->column_names( @$ra_colnames );

                my $ra_airports = $csv->getline_hr_all( $fh );

        close ( $fh );

        return $ra_airports;

}

sub get_name_matching_airports {



}

my $rah_airports = parse_airports( $filename );
my $rah_airports_found = [];

if ($matching) {
  say "Up to $number airports matching $matching in $filename:";
        $rah_airports_found = get_name_matching_airports(
        airports        => $rah_airports,
        matching_string => $matching,
        word            => $word,
  );
}
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";
}

print pp($rah_airports_found);

所以我在努力的地方是&#34; sub get_name_matching_airports&#34;

因为您没有该文件,所以让我解释一下文件结构。 它是哈希(所有IATA机场)的哈希(每个机场的详情)。每个机场哈希中大约有15个键,其中一个键标题是(NAME)。我打开了文件并将所有信息解析为一个哈希引用,该引用在sub&#34; parse_airports&#34;的末尾返回。

在sub&#34; get_name_matching_airports&#34;我需要根据我传入的参数($ match)找到其他类似名称的机场。

示例:我解析(不区分大小写)&#34;伦敦&#34;作为命令行的参数,例如./search_airports2 - 匹配伦敦。在sub&#34; get_name_matching_airports&#34;我需要回答任何在密钥(名称)中有伦敦(不区分大小写)的机场。

然后推送这些新发现的类似于阵列的机场&#34; rah_airports_found&#34;最后打印出来。

所以我用下面的代码解决了我的问题:

sub get_name_matching_airports {

        my %params = (
                airports        => undef,
                matching_string => undef,
                word            => undef,
                @_
        );

        my @rah_airports_found;

        my $ra_airports = $params{airports};
        my $counter = 0;

        foreach my $i ( @$ra_airports ) {
                if ( $params{word} ) {
                        if ( $i->{name} eq $params{matching_string} ) {
                                push @rah_airports_found, $i;
                                $counter++;
                                }
                        }
                        else {
                        if ( $i->{name} =~ /$params{matching_string}/i ) {
                                push @rah_airports_found, $i;
                                $counter++;
                        }
                        if ( defined( $number ) && $counter == $number ) {
                                return \@rah_airports_found;
                        }
                }
        }

        return \@rah_airports_found;

}

1 个答案:

答案 0 :(得分:1)

示例:

for my $Airport_rf (keys %{$rah_airports}) {
    if ( $Airport_rf->{NAME} =~ m{\Q$matching\E}xi) {
        # do your stuff here
    }
}

如果您不知道hashref的确切密钥,则必须将CLI参数与所有值匹配。