2

我有一个包含三个名字的文件:丹尼尔、伊莱恩和维多利亚。如果我搜索丹尼尔,我会得到“你不在名单上”。有人可以指出我的错误在哪里吗?谢谢你。

#!/usr/bin/perl 

#open file 
open(FILE, "names") or die("Unable to open file"); 

# read file into an array 
@data = <FILE>; 

# close file 
close(FILE); 

print "Enter name\n"; 
$entry = <STDIN>; 
chomp $entry; 

if (grep {$_ eq $entry} @data) 
{ 
print "You are on the list $entry"; 
} 
else 
{ 
print "Your are not on the list"; 
} 
4

2 回答 2

8

您还需要chomp(从每个字符串的末尾删除换行符)文件中的数据:

chomp @data;

if (grep {$_ eq $entry} @data) { 
    print "You are on the list $entry"; 
} else { 
    print "Your are not on the list"; 
} 
于 2013-05-28T11:22:21.140 回答
2

改变这个

if (grep {$_ eq $entry} @data) 

对此

if (grep {$_ =~ m/^$entry\b/i} @data)

如果您特别希望它区分大小写,请删除 i。

于 2013-05-28T11:18:43.350 回答