Perl 简单比较 == vs eq [英] Perl simple comparison == vs eq

查看:86
本文介绍了Perl 简单比较 == vs eq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于接受的答案Perl 中的字符串与eq"比较对比=="

它说首先,eq是用来比较字符串的;== 用于比较数字.

"== 进行数字比较:它将两个参数都转换为一个数字,然后比较它们."
eq 进行字符串比较:两个参数必须在词法上匹配(区分大小写)"

"== does a numeric comparison: it converts both arguments to a number and then compares them."
"eq does a string comparison: the two arguments must match lexically (case-sensitive)"

您只能使用 eq 来比较字符串,但是
eq AND == 都用于比较数字

You can ONLY use eq for comparing strings but
both eq AND == works for comparing numbers

数字是字符串的子集,所以我只是不明白你为什么会使用 ==

numbers are subset of strings so i just dont understand why you would ever use ==

您是否有理由希望使用 == 来比较数值而不是仅使用 eq 来比较所有数值?

Is there a reason why you would want to use == for comparing numeric values over just using eq for all?

推荐答案

以下是您可能需要 的原因的示例==:

$a = "3.0";
print "eq" if $a eq "3"; # this will not print
print "==" if $a == 3;   # this will print

3.0 在数字上等于 3,因此如果您希望它们相等,请使用 ==.如果要进行字符串比较,则 "3.0" 不等于 "3",因此在这种情况下,您将使用 eq.最后,== 是比 eq 更便宜的操作.

3.0 is numerically equal to 3, so if you want them to be equal, use ==. If you want to do string comparisons, then "3.0" is not equal to "3", so in this case you would use eq. Finally, == is a cheaper operation than eq.

这篇关于Perl 简单比较 == vs eq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆