在 Perl 中如何比较单个字符串?现在,我正在尝试使用“eq”:
print "Word: " . $_[0] . "\n";
print "N for noun, V for verb, and any other key if the word falls into neither category.\n";
$category = <STDIN>;
print "category is...." . $category . "\n";
if ($category eq "N")
{
print "N\n";
push (@nouns, $_[0]);
}
elsif($category eq "V")
{
print "V\n";
push (@verbs, $_[0]);
}
else
{
print "Else\n";
push(@wordsInBetween, $_[0]);
}
但它不起作用。无论输入如何,始终执行 else 块。
最佳答案
您如何接受 $category
的值?如果像 my $category = <STDIN>
那样做,您将不得不在最后通过以下方式切掉换行符:
chomp( my $category = <STDIN> );
关于perl - 为什么我对单个字符的字符串相等性测试不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1037324/