gpt4 book ai didi

Perl 中的字符串与 "eq"和 "=="进行比较

转载 作者:行者123 更新时间:2023-12-03 05:13:27 25 4
gpt4 key购买 nike

我(一个完全的 Perl 新手)正在 if 中进行字符串比较声明:

如果我执行以下操作:

if ($str1 == "taste" && $str2 == "waste") { }

我看到了正确的结果(即,如果条件匹配,它将评估“then” block )。但我看到这些警告:

Argument "taste" isn't numeric in numeric eq (==) at line number x.
Argument "waste" isn't numeric in numeric eq (==) at line number x.

但如果我这样做:

if ($str1 eq "taste" && $str2 eq "waste") { }

即使满足 if 条件,它也不会评估“then” block 。

在这里,$str1taste$str2waste .

我应该如何解决这个问题?

最佳答案

首先,eq用于比较字符串; == 用于比较数字。

Even if the "if" condition is satisfied, it doesn't evaluate the "then" block.

我认为你的问题是你的变量不包含你认为它们包含的内容。我认为您的 $str1$str2 包含类似“taste\n”之类的内容。通过在 if 之前打印来检查它们:print "str1='$str1'\n";

可以使用 chomp($str1); 函数删除尾随换行符。

关于Perl 中的字符串与 "eq"和 "=="进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com