【问题标题】:eq operator in Perl for both characters and integersPerl 中用于字符和整数的 eq 运算符
【发布时间】:2014-12-10 15:03:38
【问题描述】:

我正在使用 Perl 开发简单的密码检查程序,我正在比较两个字符串 "abc@123" 和 ""abc" 使用 eq 运算符 Perl 将两个字符串视为相等。有什么方法可以将同时包含字符和数字的字符串与另一个字符串进行比较?

【问题讨论】:

  • 'abc@123' eq 'abc'为真。
  • @ikegami 我意识到了这一点,这就是为什么我将双引号改为单引号以使语句正确。

标签: perl


【解决方案1】:

启用警告!

use warnings;
my $x = "abc@123";
print $x;

$x "abc" 你会得到一个警告:

Possible unintended interpolation of @123 in string

告诉你你已经将空数组@123 插入到你的字符串中。

正确引用:

my $x = 'abc@123';
if ( $x eq 'abc' ) {
    print "eq";
}
else {
    print "not eq";
}

如您所料,您会得到“not eq”。

正如 TLP 所说,不使用严格和警告是自找麻烦。

【讨论】:

  • 你应该补充一点,不使用use strict; use warnings;是自找麻烦。
  • 在发布“perl”问题时,这应该是一个标准的事情。
猜你喜欢
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多