Perl 字符串和 < & >运营商

发布于 2024-12-15 15:14:31 字数 279 浏览 1 评论 0 原文

对于比较字符串,通常使用 lt、gt 等。

当我使用数字运算符比较字符串时,perl 到底做了什么? (<, >)

my $str1 = 'Joe';
my $str2 = 'flight';

我猜当执行 $str1 gt $str2 perl 时会比较 ASCII 代码(?),但是当我执行以下操作时会发生什么:

$str1 > $str2

谢谢

For comparing strings, the use is usually lt,gt etc..

what exactly perl does when I compare strings using numerical operators? (<, >)

my $str1 = 'Joe';
my $str2 = 'flight';

I guess that when doing $str1 gt $str2 perl compares the ASCII codes(?), but what happens when I do the following:

$str1 > $str2

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洛阳烟雨空心柳 2024-12-22 15:14:31

首先你得到一个警告:

Argument "flight" isn't numeric in numeric lt (<) at 
Argument "Joe" isn't numeric in numeric lt (<) at

然后perl将这些字符串转换为整数,这样它们就变成0,然后比较就完成了。

First of all you got a warning :

Argument "flight" isn't numeric in numeric lt (<) at 
Argument "Joe" isn't numeric in numeric lt (<) at

Then perl cast these strings to integer so they become 0 then the comparison is done.

讽刺将军 2024-12-22 15:14:31

对于 Perl 来说,10 就是 10,无论它存储为字符串 (PV)、有符号整型 (IV)、无符号整型 (UV) 还是浮点数 (NV)。

<>== 比较其操作数的数值。那么不是数字的东西的数值为零*,因此 flight 的数值为零(有警告),Joe 的数值为零(有警告),所以它们是平等的。

另一方面,字符串 10 的数值是 10,字符串 2 的数值是 2,因此

10 >= 2       # True
'10' >= '2'   # True

10 ge 2       # False (ord('1') is less than ord('2'))
'10' ge '2'   # False

* — 对象可以覆盖它,并且数值引用的地址是引用值的地址。

To Perl, 10 is 10 whether it's stored as a string (PV), a signed int (IV), an unsigned int (UV) or a floating point number (NV).

<, > and == compare the numerical values of their operands. Then numerical value of stuff that isn't a number is zero*, so the numerical value of flight is zero (with a warning) and the numerical value of Joe is zero (with a warning), so they are equal.

On the other hand, the numerical value of string 10 is 10, and the numerical value of string 2 is 2, so

10 >= 2       # True
'10' >= '2'   # True

10 ge 2       # False (ord('1') is less than ord('2'))
'10' ge '2'   # False

* — Objects can override this, and the numerical value of a reference is the address of the referenced value.

双马尾 2024-12-22 15:14:31
what exactly perl does when I compare strings using numerical operators? (<, >)

perl 在比较之前将字符串转换为数字。

您的两个示例字符串都转换为零。

but what happens when I do the following:
$str1 > $str2

它们根据“整理顺序”(通常按字母顺序)进行比较,
就像在电话簿中一样对字符串进行排序。

'barney' < 'bammbam'

是假的:

compare 1st letters - tied
compare 2nd letters - tied
compare 3rd letters - "m" comes before "r", so the comparison is false.
what exactly perl does when I compare strings using numerical operators? (<, >)

perl converts the strings to numbers before comparing them.

Both of your example strings convert to zero.

but what happens when I do the following:
$str1 > $str2

They are compared according to the "collating sequence" (nominally alphabetical),
as if the strings were sorted as in a phonebook.

'barney' < 'bammbam'

is false:

compare 1st letters - tied
compare 2nd letters - tied
compare 3rd letters - "m" comes before "r", so the comparison is false.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文