string Perl 中的字符串比较“eq”与“==”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14046669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String compare in Perl with "eq" vs "=="
提问by hari
I am (a complete Perl newbie) doing string compare in an if
statement:
我是(一个完整的 Perl 新手)在一个if
语句中进行字符串比较:
If I do following:
如果我执行以下操作:
if ($str1 == "taste" && $str2 == "waste") { }
I see the correct result (i.e. if the condition matches, it evaluates the "then" block). But I see these warnings:
我看到了正确的结果(即,如果条件匹配,则评估“then”块)。但我看到这些警告:
Argument "taste" isn't numeric in numeric eq (==) at line number x.
Argument "waste" isn't numeric in numeric eq (==) at line number x.
在第 x 行的数字 eq (==) 中,参数“taste”不是数字。
在第 x 行的数字 eq (==) 中,参数“waste”不是数字。
But if I do:
但如果我这样做:
if ($str1 eq "taste" && $str2 eq "waste") { }
Even if the if condition is satisfied, it doesn't evaluate the "then" block.
即使满足 if 条件,它也不会评估“then”块。
Here, $str1
is taste
and $str2
is waste
.
在这里,$str1
是taste
和$str2
是waste
。
How should I fix this?
我应该如何解决这个问题?
回答by PSIAlt
First, eqis for comparing strings; ==is for comparing numbers.
首先,eq用于比较字符串;==用于比较数字。
Even if the "if" condition is satisfied, it doesn't evaluate the "then" block.
即使满足“if”条件,它也不会评估“then”块。
I think your problem is that your variables don't contain what you think they do. I think your $str1
or $str2
contains something like "taste\n" or so. Check them by printing before your if: print "str1='$str1'\n";
.
我认为您的问题是您的变量不包含您认为它们的作用。我认为您的$str1
或$str2
包含诸如“味道\n”之类的东西。通过在if之前打印来检查它们:print "str1='$str1'\n";
。
The trailing newline can be removed with the chomp($str1);
function.
可以使用该chomp($str1);
函数删除尾随的换行符。
回答by cdhowie
==
does a numeric comparison: it converts both arguments to a number and then compares them. As long as $str1
and $str2
both evaluate to 0 as numbers, the condition will be satisfied.
==
进行数字比较:它将两个参数转换为一个数字,然后比较它们。只要$str1
和$str2
都作为数字计算为 0,则条件将被满足。
eq
does a string comparison: the two arguments must match lexically (case-sensitive) for the condition to be satisfied.
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.
回答by user4185253
Did you try to chomp the $str1
and $str2
?
你有没有尝试 chomp$str1
和$str2
?
I found a similar issue with using (another) $str1
eq 'Y' and it only went away when I first did:
我在使用(另一个)$str1
eq 'Y' 时发现了一个类似的问题,它只在我第一次这样做时就消失了:
chomp($str1);
if ($str1 eq 'Y') {
....
}
works after that.
在那之后工作。
Hope that helps.
希望有帮助。
回答by user1931990
Maybe the condition you are using is incorrect:
也许您使用的条件不正确:
$str1 == "taste" && $str2 == "waste"
The program will enter into THEN
part only when both of the stated conditions are true.
THEN
只有当上述两个条件都为真时,该计划才会生效。
You can try with $str1 == "taste" || $str2 == "waste"
. This will execute the THEN
part if anyone of the above conditions are true.
您可以尝试使用$str1 == "taste" || $str2 == "waste"
. THEN
如果上述条件中的任何一个为真,这将执行该部分。