如何在 Perl 中比较两个字符串?
如何在 Perl 中比较两个字符串?
我正在学习 Perl,我在 StackOverflow 上查找了这个基本问题,但没有找到好的答案,所以我想我会问。
How do I compare two strings in Perl?
I am learning Perl, I had this basic question looked it up here on StackOverflow and found no good answer so I thought I would ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请参阅 perldoc perlop。 根据字符串的情况使用
lt
、gt
、eq
、ne
和cmp
比较:See perldoc perlop. Use
lt
,gt
,eq
,ne
, andcmp
as appropriate for string comparisons:cmp
比较<前><代码>'a' cmp 'b' # -1
'b' cmp 'a' # 1
'a' cmp 'a' # 0
eq
等于ne
不等于<前><代码>'a' 和 'b' # 1
'b' 和 'a' # 1
'一个' '一个' # 0
lt
小于<前><代码>'a' lt 'b' # 1
'b' lt 'a' # 0
'a' lt 'a' # 0
le
小于或等于gt
大于ge
大于或等于<前><代码>'a' ge 'b' # 0
'b' ge 'a' # 1
'一个' '一个' # 1
参阅
perldoc perlop
了解更多信息。(我稍微简化了一点,因为除了
cmp
之外的所有值都返回一个空字符串和一个数字零值而不是0
的值,以及一个值既是字符串'1'
又是数值1
。这些值与 Perl 中的布尔运算符始终得到的值相同。您实际上应该只使用 return。布尔或数字运算的值,在这种情况下差异并不重要)。cmp
Compareeq
Equal tone
Not-Equal tolt
Less thanle
Less than or equal togt
Greater thange
Greater than or equal toSee
perldoc perlop
for more information.( I'm simplifying this a little bit as all but
cmp
return a value that is both an empty string, and a numerically zero value instead of0
, and a value that is both the string'1'
and the numeric value1
. These are the same values you will always get from boolean operators in Perl. You should really only be using the return values for boolean or numeric operations, in which case the difference doesn't really matter. )除了 Sinan Ünür 全面列出的字符串比较运算符之外,Perl 5.10 还添加了智能匹配运算符。
智能匹配运算符根据两个项目的类型进行比较。 请参阅下面的图表了解 5.10 的行为(我相信此行为在 5.10.1 中略有变化):
perldoc perlsyn
“智能匹配详解”:In addtion to Sinan Ünür comprehensive listing of string comparison operators, Perl 5.10 adds the smart match operator.
The smart match operator compares two items based on their type. See the chart below for the 5.10 behavior (I believe this behavior is changing slightly in 5.10.1):
perldoc perlsyn
"Smart matching in detail":这个问题的明显潜台词是:
Perl 对于文本和数字没有不同的数据类型。 它们都由类型 “标量” 表示。 换句话说,字符串是数字if你就这样使用它们。
由于语言无法区分文本和数字,因此我们不能简单地重载
==
运算符来对这两种情况执行正确的操作。 因此,Perl 提供了eq
来将值与文本进行比较:简而言之:
==
或!=
,将两个操作数作为数字进行比较eq
或ne
,将两个操作数作为文本进行比较还有许多其他函数和运算符可用于比较标量值,但了解这两种形式之间的区别是重要的第一步。
The obvious subtext of this question is:
Perl doesn't have distinct data types for text vs. numbers. They are both represented by the type "scalar". Put another way, strings are numbers if you use them as such.
Since text and numbers aren't differentiated by the language, we can't simply overload the
==
operator to do the right thing for both cases. Therefore, Perl provideseq
to compare values as text:In short:
==
or!=
, to compare two operands as numberseq
orne
, to compare two operands as textThere are many other functions and operators that can be used to compare scalar values, but knowing the distinction between these two forms is an important first step.
Perl 有单独的字符串比较和数字比较运算符来帮助语言中的松散类型。 您应该阅读 perlop 了解所有不同的运算符。
Perl has seperate string comparison and numeric comparison operators to help with the loose typing in the language. You should read perlop for all the different operators.
如果您想提取两个字符串之间的差异,可以使用 String::差异。
And if you'd like to extract the differences between the two strings, you can use String::Diff.
我来寻找一个解决方案,在 Perl 中我可以比较 A > > B或Z< AA。 这里没有任何东西对我来说可靠,所以我想出了自己的解决方案。 技巧是为每个字母分配一个数字,
例如,
然后当时间到了比较是否 A > 时。 B 你得到相应的数字并比较它们,在这种情况下 1 > B 2
这是工作 perl 代码。
I came looking for a solution where in perl I could compare if A > B or Z < AA. Nothing here worked reliably for me so I came up with my own solution. The tricks is to assign a number for each letter
For example
Then when time comes to compare if A > B you get the corresponding numbers and compare them in this case 1 > 2
Heres working perl code.