每当我输入内容时,代码总是告诉我它存在.但我知道有些输入不存在.怎么了?
#!/usr/bin/perl
@array = <>;
print "Enter the word you what to match\n";
chomp($match = <STDIN>);
if (grep($match, @array)) {
print "found it\n";
}
Run Code Online (Sandbox Code Playgroud) 是否存在Perl习惯用法,用于在符合特定条件的数组中查找项目(如果有)?
my $match = 0;
foreach(@list){
if (match_test($_)){
$result = $_;
$match = 1;
last;
}
}
$match || die("No match.");
say $result, " is a match.";
Run Code Online (Sandbox Code Playgroud)
这个例子看起来有点尴尬.我希望Perl能够更干净地处理这个问题.