我如何比较Perl中的两个字符串?

时间:2021-07-01 21:50:01

How do I compare two strings in Perl?

我如何比较Perl中的两个字符串?

I am learning Perl, I had this basic question looked it up here on * and found no good answer so I thought I would ask.

我正在学习Perl,我有一个基本的问题,在*上查了一下,没有找到好的答案,所以我想我会问。

6 个解决方案

#1


146  

See perldoc perlop. Use lt, gt, eq, ne, and cmp as appropriate for string comparisons:

看到perldoc perlop。使用lt、gt、eq、ne和cmp进行字符串比较:

Binary eq returns true if the left argument is stringwise equal to the right argument.

如果左参数与正确的参数相等,则二进制eq返回true。

Binary ne returns true if the left argument is stringwise not equal to the right argument.

如果左参数不等于正确的参数,则二进制返回true。

Binary cmp returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.

二进制cmp返回-1、0或1,这取决于左参数是否小于、等于或大于正确的参数。

Binary ~~ does a smartmatch between its arguments. ...

二进制~~在它的参数之间有一个smartmatch。

lt, le, ge, gt and cmp use the collation (sort) order specified by the current locale if a legacy use locale (but not use locale ':not_characters') is in effect. See perllocale. Do not mix these with Unicode, only with legacy binary encodings. The standard Unicode::Collate and Unicode::Collate::Locale modules offer much more powerful solutions to collation issues.

lt、le、ge、gt和cmp使用当前地区指定的排序规则(排序),如果遗留使用地区(但不使用locale ':not_characters')将生效。看到perllocale。不要将它们与Unicode混合,只使用遗留的二进制编码。标准的Unicode::Collate和Unicode::Collate::Locale模块为排序问题提供了更强大的解决方案。

#2


118  

  • cmp Compare

    cmp比较

    'a' cmp 'b' # -1
    'b' cmp 'a' #  1
    'a' cmp 'a' #  0
    
  • eq Equal to

    情商等于

    'a' eq  'b' #  0
    'b' eq  'a' #  0
    'a' eq  'a' #  1
    
  • ne Not-Equal to

    不不相等,

    'a' ne  'b' #  1
    'b' ne  'a' #  1
    'a' ne  'a' #  0
    
  • lt Less than

    lt不到

    'a' lt  'b' #  1
    'b' lt  'a' #  0
    'a' lt  'a' #  0
    
  • le Less than or equal to

    小于或等于。

    'a' le  'b' #  1
    'b' le  'a' #  0
    'a' le  'a' #  1
    
  • gt Greater than

    gt大于

    'a' gt  'b' #  0
    'b' gt  'a' #  1
    'a' gt  'a' #  0
    
  • ge Greater than or equal to

    大于或等于。

    'a' ge  'b' #  0
    'b' ge  'a' #  1
    'a' ge  'a' #  1
    

See perldoc perlop for more information.

有关更多信息,请参见perldoc perlop。

( 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 of 0, and a value that is both the string '1' and the numeric value 1. 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. )

(我把它简化了一点,但是cmp返回的值是空字符串,而数值为0,而不是0,它的值是字符串'1'和数值1。这些值与Perl中的布尔运算符相同。您实际上应该只使用布尔值或数值操作的返回值,在这种情况下,差异并不重要。

#3


15  

In addtion to Sinan Ünür comprehensive listing of string comparison operators, Perl 5.10 adds the smart match operator.

在Sinan Unur的字符串比较运算符的综合列表中,Perl 5.10添加了智能匹配操作符。

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):

智能匹配操作符根据它们的类型比较两个项目。请看下面的5.10行为的图表(我认为这一行为在5.10.1中略有变化):

perldoc perlsyn "Smart matching in detail":

The behaviour of a smart match depends on what type of thing its arguments are. It is always commutative, i.e. $a ~~ $b behaves the same as $b ~~ $a . The behaviour is determined by the following table: the first row that applies, in either order, determines the match behaviour.

智能匹配的行为取决于其参数的类型。它总是可交换的,即$a ~~ $b的行为与$b ~~ $a的行为相同。行为是由下表决定的:第一行在任何一个顺序中都决定了匹配行为。

  $a      $b        Type of Match Implied    Matching Code
  ======  =====     =====================    =============
  (overloading trumps everything)

  Code[+] Code[+]   referential equality     $a == $b   
  Any     Code[+]   scalar sub truth         $b−>($a)   

  Hash    Hash      hash keys identical      [sort keys %$a]~~[sort keys %$b]
  Hash    Array     hash slice existence     grep {exists $a−>{$_}} @$b
  Hash    Regex     hash key grep            grep /$b/, keys %$a
  Hash    Any       hash entry existence     exists $a−>{$b}

  Array   Array     arrays are identical[*]
  Array   Regex     array grep               grep /$b/, @$a
  Array   Num       array contains number    grep $_ == $b, @$a 
  Array   Any       array contains string    grep $_ eq $b, @$a 

  Any     undef     undefined                !defined $a
  Any     Regex     pattern match            $a =~ /$b/ 
  Code()  Code()    results are equal        $a−>() eq $b−>()
  Any     Code()    simple closure truth     $b−>() # ignoring $a
  Num     numish[!] numeric equality         $a == $b   
  Any     Str       string equality          $a eq $b   
  Any     Num       numeric equality         $a == $b   

  Any     Any       string equality          $a eq $b   

+ − this must be a code reference whose prototype (if present) is not ""
(subs with a "" prototype are dealt with by the 'Code()' entry lower down) 
* − that is, each element matches the element of same index in the other
array. If a circular reference is found, we fall back to referential 
equality.   
! − either a real number, or a string that looks like a number

The "matching code" doesn't represent the real matching code, of course: it's just there to explain the intended meaning. Unlike grep, the smart match operator will short-circuit whenever it can.

当然,“匹配代码”并不代表真正的匹配代码:它只是用来解释预期的含义。不像grep,聪明的匹配操作人员会在任何时候短路。

Custom matching via overloading You can change the way that an object is matched by overloading the ~~ operator. This trumps the usual smart match semantics. See overload.

通过重载自定义匹配可以改变对象的匹配方式,即重载~~操作符。这胜过通常的智能匹配语义。看到超载。

#4


9  

print "Matched!\n" if ($str1 eq $str2)

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.

Perl通过分离字符串比较和数字比较运算符来帮助语言中的松散类型。您应该为所有不同的操作符读取perlop。

#5


4  

The obvious subtext of this question is:

这个问题的明显潜台词是:

why can't you just use == to check if two strings are the same?

为什么不能用==检查两个字符串是否相同?

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.

Perl没有针对文本和数字的不同数据类型。它们都由类型“标量”表示。换句话说,如果你使用字符串,那么字符串就是数字。

if ( 4 == "4" ) { print "true"; } else { print "false"; }
true

if ( "4" == "4.0" ) { print "true"; } else { print "false"; }
true

print "3"+4
7

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 provides eq to compare values as text:

由于文本和数字没有被语言区分,我们不能简单地重载==操作符来为这两种情况做正确的事情。因此,Perl提供了将值作为文本进行比较的方法:

if ( "4" eq "4.0" ) { print "true"; } else { print "false"; }
false

if ( "4.0" eq "4.0" ) { print "true"; } else { print "false"; }
true

In short:

简而言之:

  • Perl doesn't have a data-type exclusively for text strings
  • Perl没有专门用于文本字符串的数据类型。
  • use == or !=, to compare two operands as numbers
  • 使用==或!=将两个操作数作为数字比较。
  • use eq or ne, to compare two operands as text
  • 使用eq或ne,比较两个操作数作为文本。

There 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.

还有许多其他函数和运算符可以用来比较标量值,但是知道这两种形式之间的区别是重要的第一步。

#6


1  

And if you'd like to extract the differences between the two strings, you can use String::Diff.

如果您想要提取两个字符串之间的差异,可以使用String::Diff。

#1


146  

See perldoc perlop. Use lt, gt, eq, ne, and cmp as appropriate for string comparisons:

看到perldoc perlop。使用lt、gt、eq、ne和cmp进行字符串比较:

Binary eq returns true if the left argument is stringwise equal to the right argument.

如果左参数与正确的参数相等,则二进制eq返回true。

Binary ne returns true if the left argument is stringwise not equal to the right argument.

如果左参数不等于正确的参数,则二进制返回true。

Binary cmp returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.

二进制cmp返回-1、0或1,这取决于左参数是否小于、等于或大于正确的参数。

Binary ~~ does a smartmatch between its arguments. ...

二进制~~在它的参数之间有一个smartmatch。

lt, le, ge, gt and cmp use the collation (sort) order specified by the current locale if a legacy use locale (but not use locale ':not_characters') is in effect. See perllocale. Do not mix these with Unicode, only with legacy binary encodings. The standard Unicode::Collate and Unicode::Collate::Locale modules offer much more powerful solutions to collation issues.

lt、le、ge、gt和cmp使用当前地区指定的排序规则(排序),如果遗留使用地区(但不使用locale ':not_characters')将生效。看到perllocale。不要将它们与Unicode混合,只使用遗留的二进制编码。标准的Unicode::Collate和Unicode::Collate::Locale模块为排序问题提供了更强大的解决方案。

#2


118  

  • cmp Compare

    cmp比较

    'a' cmp 'b' # -1
    'b' cmp 'a' #  1
    'a' cmp 'a' #  0
    
  • eq Equal to

    情商等于

    'a' eq  'b' #  0
    'b' eq  'a' #  0
    'a' eq  'a' #  1
    
  • ne Not-Equal to

    不不相等,

    'a' ne  'b' #  1
    'b' ne  'a' #  1
    'a' ne  'a' #  0
    
  • lt Less than

    lt不到

    'a' lt  'b' #  1
    'b' lt  'a' #  0
    'a' lt  'a' #  0
    
  • le Less than or equal to

    小于或等于。

    'a' le  'b' #  1
    'b' le  'a' #  0
    'a' le  'a' #  1
    
  • gt Greater than

    gt大于

    'a' gt  'b' #  0
    'b' gt  'a' #  1
    'a' gt  'a' #  0
    
  • ge Greater than or equal to

    大于或等于。

    'a' ge  'b' #  0
    'b' ge  'a' #  1
    'a' ge  'a' #  1
    

See perldoc perlop for more information.

有关更多信息,请参见perldoc perlop。

( 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 of 0, and a value that is both the string '1' and the numeric value 1. 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. )

(我把它简化了一点,但是cmp返回的值是空字符串,而数值为0,而不是0,它的值是字符串'1'和数值1。这些值与Perl中的布尔运算符相同。您实际上应该只使用布尔值或数值操作的返回值,在这种情况下,差异并不重要。

#3


15  

In addtion to Sinan Ünür comprehensive listing of string comparison operators, Perl 5.10 adds the smart match operator.

在Sinan Unur的字符串比较运算符的综合列表中,Perl 5.10添加了智能匹配操作符。

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):

智能匹配操作符根据它们的类型比较两个项目。请看下面的5.10行为的图表(我认为这一行为在5.10.1中略有变化):

perldoc perlsyn "Smart matching in detail":

The behaviour of a smart match depends on what type of thing its arguments are. It is always commutative, i.e. $a ~~ $b behaves the same as $b ~~ $a . The behaviour is determined by the following table: the first row that applies, in either order, determines the match behaviour.

智能匹配的行为取决于其参数的类型。它总是可交换的,即$a ~~ $b的行为与$b ~~ $a的行为相同。行为是由下表决定的:第一行在任何一个顺序中都决定了匹配行为。

  $a      $b        Type of Match Implied    Matching Code
  ======  =====     =====================    =============
  (overloading trumps everything)

  Code[+] Code[+]   referential equality     $a == $b   
  Any     Code[+]   scalar sub truth         $b−>($a)   

  Hash    Hash      hash keys identical      [sort keys %$a]~~[sort keys %$b]
  Hash    Array     hash slice existence     grep {exists $a−>{$_}} @$b
  Hash    Regex     hash key grep            grep /$b/, keys %$a
  Hash    Any       hash entry existence     exists $a−>{$b}

  Array   Array     arrays are identical[*]
  Array   Regex     array grep               grep /$b/, @$a
  Array   Num       array contains number    grep $_ == $b, @$a 
  Array   Any       array contains string    grep $_ eq $b, @$a 

  Any     undef     undefined                !defined $a
  Any     Regex     pattern match            $a =~ /$b/ 
  Code()  Code()    results are equal        $a−>() eq $b−>()
  Any     Code()    simple closure truth     $b−>() # ignoring $a
  Num     numish[!] numeric equality         $a == $b   
  Any     Str       string equality          $a eq $b   
  Any     Num       numeric equality         $a == $b   

  Any     Any       string equality          $a eq $b   

+ − this must be a code reference whose prototype (if present) is not ""
(subs with a "" prototype are dealt with by the 'Code()' entry lower down) 
* − that is, each element matches the element of same index in the other
array. If a circular reference is found, we fall back to referential 
equality.   
! − either a real number, or a string that looks like a number

The "matching code" doesn't represent the real matching code, of course: it's just there to explain the intended meaning. Unlike grep, the smart match operator will short-circuit whenever it can.

当然,“匹配代码”并不代表真正的匹配代码:它只是用来解释预期的含义。不像grep,聪明的匹配操作人员会在任何时候短路。

Custom matching via overloading You can change the way that an object is matched by overloading the ~~ operator. This trumps the usual smart match semantics. See overload.

通过重载自定义匹配可以改变对象的匹配方式,即重载~~操作符。这胜过通常的智能匹配语义。看到超载。

#4


9  

print "Matched!\n" if ($str1 eq $str2)

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.

Perl通过分离字符串比较和数字比较运算符来帮助语言中的松散类型。您应该为所有不同的操作符读取perlop。

#5


4  

The obvious subtext of this question is:

这个问题的明显潜台词是:

why can't you just use == to check if two strings are the same?

为什么不能用==检查两个字符串是否相同?

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.

Perl没有针对文本和数字的不同数据类型。它们都由类型“标量”表示。换句话说,如果你使用字符串,那么字符串就是数字。

if ( 4 == "4" ) { print "true"; } else { print "false"; }
true

if ( "4" == "4.0" ) { print "true"; } else { print "false"; }
true

print "3"+4
7

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 provides eq to compare values as text:

由于文本和数字没有被语言区分,我们不能简单地重载==操作符来为这两种情况做正确的事情。因此,Perl提供了将值作为文本进行比较的方法:

if ( "4" eq "4.0" ) { print "true"; } else { print "false"; }
false

if ( "4.0" eq "4.0" ) { print "true"; } else { print "false"; }
true

In short:

简而言之:

  • Perl doesn't have a data-type exclusively for text strings
  • Perl没有专门用于文本字符串的数据类型。
  • use == or !=, to compare two operands as numbers
  • 使用==或!=将两个操作数作为数字比较。
  • use eq or ne, to compare two operands as text
  • 使用eq或ne,比较两个操作数作为文本。

There 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.

还有许多其他函数和运算符可以用来比较标量值,但是知道这两种形式之间的区别是重要的第一步。

#6


1  

And if you'd like to extract the differences between the two strings, you can use String::Diff.

如果您想要提取两个字符串之间的差异,可以使用String::Diff。