我是(一个完整的Perl新手)在if声明中进行字符串比较:
如果我这样做:
if ($str1 == "taste" && $str2 == "waste") { }
Run Code Online (Sandbox Code Playgroud)
我看到了正确的结果(即如果条件匹配,则评估"then"块).但我看到这些警告:
参数"taste"在行号x的数字eq(==)中不是数字.
参数"waste"在行号x的数字eq(==)中不是数字.
但如果我这样做:
if ($str1 eq "taste" && $str2 eq "waste") { }
Run Code Online (Sandbox Code Playgroud)
即使满足if条件,它也不会评估"then"块.
在这里,$str1是taste和$str2是waste.
我该怎么解决这个问题?
PSI*_*Alt 106
首先,eq用于比较字符串; ==用于比较数字.
即使满足"if"条件,它也不会评估"then"块.
我认为你的问题是你的变量不包含你认为他们做的事情.我认为你$str1或者$str2包含类似"味道"的东西.在if:之前打印检查它们print "str1='$str1'\n";.
可以使用该chomp($str1);功能删除尾随换行符.
cdh*_*wie 28
==做一个数字比较:它将两个参数转换为数字,然后比较它们.只要$str1并且$str2两者都作为数字评估为0,则满足条件.
eq 进行字符串比较:两个参数必须符合词条(区分大小写)才能满足条件.
"foo" == "bar"; # True, both strings evaluate to 0.
"foo" eq "bar"; # False, the strings are not equivalent.
"Foo" eq "foo"; # False, the F characters are different cases.
"foo" eq "foo"; # True, both strings match exactly.
Run Code Online (Sandbox Code Playgroud)
小智 5
你有没有试着扼杀$str1和$str2?
我发现了使用(另一个)$str1eq'Y' 的类似问题,它只在我第一次出现时才消失:
chomp($str1);
if ($str1 eq 'Y') {
....
}
Run Code Online (Sandbox Code Playgroud)
之后工作.
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
303222 次 |
| 最近记录: |