Perl如何比较标量?介绍和代码示例

2021年3月23日14:20:05 发表评论 676 次浏览

先决条件:Perl中的标量

Perl有两种比较操作符集。与其他数学运算符一样,这些运算符不是执行操作,而是比较标量。Perl比较操作符有两种类型。

一个用于数值标量值,一个用于字符串标量值。下表说明了这两种类型:

数字 String 描述
== eq 等于
!= ne 不等于
< lt 小于
> gt 大于
<= le 小于或等于
> = ge 大于或等于

对以上数字和字符串标量比较操作符的解释:

==和eq:此运算符用于检查是否相等。在下面的代码中, 将比较使用==和eq之后的代码输出, 并显示其对于数字和字符串标量的不同工作方式。

范例1:

# Perl program to illustrate
# == operator
  
# taking two numeric scalars
$x = 5;
$y = 5;
  
# using "==" operator
if ( $x == $y )
{
     print "== works with numeric value!" ;
}

输出如下:

== works with numeric value!

范例2:

# Perl program to illustrate
# == and eq operator
  
# string scalar
$str = "geekforgeeks" ;
  
if ( $str == "lsbin" )
{
     print "== doesn't work with string values!" ;
  
}
  
# comparing with capital string
if ( $str eq "lsbin" )
{
     print "eq works with string values!" ;
}

输出如下:

== doesn't work with string values!

说明:在示例2的输出中, 由于$ str和lsbin不相等, 因此不会执行Last print语句。另外, " g"和" G"的ASCII码是不同的。因此, ==适用于数字值, 但对于字符串值无效, 而eq仅适用于字符串标量。

!=和ne:在下面的代码中, 对使用!=和ne后的输出进行了比较, 并显示了哪个对字符串正确工作, 哪个对数字标量值正确工作。

范例1:

# Perl program to demonstrate the 
# != operator
  
# numeric scalars
$x = 5;
$y = 10;
  
# using != operator
if ( $x != $y )
{
     print "!= works with numeric value!" ;
}

输出如下:

!= works with numeric value!

范例2:

# Perl program to demonstrate the 
# != and ne operator
  
# string scalar
$str = "geekforgeeks" ;
  
# using != operator
if ( $str != "lsbin" )
{
     print "\n!= doesn't work with string values!" ;
  
}
  
# comparing with capital string
if ( $str ne "lsbin" )
{
     print "ne works with string values!" ;
}

输出如下:

ne works with string values!

说明:在第二个示例中, 将不会执行第一个print语句, 因为!=将两个字符串都转换为0。因此, !=适用于数字值, 但对于字符串值则失败, 而ne适用于字符串标量。

(>或gt)和(<或lt)

在下面的代码中, 我们将在使用(>或gt)和(<或lt)之后比较输出, 并查看哪个对字符串正确工作, 哪个对数字标量值正确工作。

范例1:

# Perl program to demonstrate the 
# (> or gt) And (< or lt)
# operator
  
# numeric scalars
$x = 4;
$y = 5;
  
# using (> or gt) And (< or lt)
if (( $x < $y ) and ( $x lt $y ) )
{
     print "< and lt works with numeric value!" ;
}
  
if (( $y > $x ) and ( $y gt $x ) )
{
     print "\n> and gt works with numeric value!" ;
}

输出如下:

< and lt works with numeric value!
> and gt works with numeric value!

范例2:

# Perl program to demonstrate the 
# (> or gt) And (< or lt)
# operator
  
# string scalar
$str = "geekforgeeks" ;
  
if ( $str < "lsbin" )
{
     print "< doesn't work with string values!" ;
}
  
# comparing with capital string
if ( $str lt "lsbinZZZ" )
{
     print "lt works with string values!" ;
}
  
# comparing with capital string
if ( $str gt "lsbinZZZ" )
{
     print "gt works with string values!" ;
}
  
# comparing with capital string
if ( $str lt "kEEKSFORGEEKS" )
{
     print "\nlt works with string values!" ;
}

输出如下:

gt works with string values!
lt works with string values!

说明:

上面的代码告诉我们有关Perl如何使用字符串的一些有趣的事情。第一个示例的输出非常明显, 因为字符串和数字运算符都以相同的方式对待数字标量。

但是在第二个输出中, " lt"的表现不符合我们的预期。假设Perl的" lt"运算符不区分大小写, 但我们甚至在其后加上" ZZZ", 即使在这种情况下, $ str也不小于带引号的字符串, 并且下一个输出显示该字符串更大。从第二个示例的第二行输出可以清楚地看出这一点

Perl的字符串运算符仅首先检查String的第一个字符, 然后比较ASCII代码。由于印刷体字母在ASCII表中排在第一位。 Perl编译器匹配第一个字母, 然后匹配其余字母。

(> =或ge)和(<=或le):这些运算符还处理在字符串运算符情况下检查的ASCII值。对于数字运算符, 将检查该值。

例子:

# Perl program to demonstrate the 
# (>= or ge) And (<= or le)
# operator
  
# numeric scalars
$x = 5;
$y = 10;
  
if (( $x <= $y ) and ( $y >= $x ))
{
     print "<= and>= works" ;
}
  
# string scalar
$str = "lsbin" ;
  
if (( $str le "keeksforgeeks" ) and ( $str ge "feeksforgeeks" ))
{
     print "\nle and ge works!" ;
}

输出如下:

<= and>= works
le and ge works!

要记住的要点:

数字运算符将始终将String值转换为0。

当我们将两个字符串标量与==, > =或<=等数值运算符进行比较时, 它将始终将标量转换为0或0.0。由于它们不是字符串。因此, 在==, > =或<=的情况下将是正确的, 如以下示例所示:

# Perl program to illustrate 
# above point
  
# numeric scalars
$x = "BBB" ;
$y = "aaa" ;
  
if (( $x == $y and ( $x <= $y ) and ( $x >= $y )))
{
     print "True" ;
}

输出如下:

True

说明:在上面的代码中, " aaa"在各个方面都小于BBB(小写, 并且a的ASCII都大于B), 但是两个字符串仍然相等, 因为数值比较运算符将字符串转换为0。

字符串运算符不比较数字值, 而是比较那里的ASCII值。

字符串运算符将ASCII值与数字值进行比较。在以下示例中, " 9 gt 17"为true, 但" 17 gt 9"将结果为false。

# Perl program to illustrate 
# above point
  
# numeric scalar
$x = 9;
$y = 17;
  
if ( $x gt $y )
{
     print "True" ;
}

输出如下:

True

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: