我正在尝试创建一个程序,允许用户输入名称,然后打印名称以及是否在文件的列中找到它以及找到它的次数。我到目前为止允许用户输入名称,但由于名称都有空格,因此返回找不到名称,因为它无法检测到确切的字符串。这就是我所拥有的:
#!/usr/bin/perl
use warnings;
use strict;
my %author;
my $input;
my $firstauthor;
open ($input, "<", 'gwas_catalog_v1.0-downloaded_2015-07-21.tsv') || die();
while (<$input>) {
my @r = split (/\t/);
my $firstauthor = $r[2];
$author{"$firstauthor"}++;
}
print "\nEnter a name: ";
my $usrin = <STDIN>;
chomp $usrin;
if (exists $author{"$usrin"}) {
print "$usrin seen $author{"$usrin"} time(s).\n\n";
}
if (!exists $author{"$usrin"}) {
print "$usrin isn't the author of anything contained in this file.\n\n";
}
通过在$usrin
内添加$author{"$usrin"}
周围的引号来解决问题。