📜  珀尔 |比较标量

📅  最后修改于: 2022-05-13 01:54:29.073000             🧑  作者: Mango

珀尔 |比较标量

先决条件:Perl 中的标量

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

一种用于数值标量值,一种用于字符串标量值。两种类型如下表所示:

NumericStringDescription
==eqEquals to
!=neNot Equals to
<ltIs less than
>gtIs greater than
<=leIs less than or equal to
>=geIs greater than or equal to

上述数值和字符串标量比较运算符的说明:

  • == 和 eq :此运算符用于检查相等性。在下面的代码中,比较了使用 == 和 eq 之后的代码输出,并显示了它对数字和字符串标量的不同工作方式。
    示例 1:
Perl
# Perl program to illustrate
# == operator
   
# taking two numeric scalars
$x = 5;
$y = 5;
   
# using "==" operator
if($x == $y)
{
    print "== works with numeric value!";
}


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


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


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


Perl
# 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!";
}


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


Perl
# 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= "geeksforgeeks";
   
if (($str le "keeksforgeeks") and ($str ge "feeksforgeeks"))
{
    print "\nle and ge works!";
}


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


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


输出:

== works with numeric value!

示例 2:

Perl

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

输出:

== doesn't work with string values!

说明:在示例 2 的输出中,不会执行 Last print 语句,因为 $str 和 GEEKSFORGEEKS 不相等。此外,“g”和“G”的 ASCII 码不同。因此, == 适用于数值,但在字符串值的情况下失败,而 eq 仅适用于字符串标量。

  • != 和 ne
    在下面的代码中,比较了使用 != 和 ne 后的输出,并显示了哪个适用于字符串,哪个适用于数字标量值。
    示例 1:

Perl

# 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

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

输出:

ne works with string values!

说明:在第二个例子中,第一个打印语句将不会被执行,因为 != 将两个字符串都转换为 0。因此,!= 对数值有效,但在字符串值的情况下失败,而 ne 对字符串标量有效。

  • (> 或 gt) 和 (< 或 lt)
    在下面的代码中,我们将比较使用 (> 或 gt) 和 (< 或 lt) 后的输出,并查看哪个适用于字符串,哪个适用于数字标量值。
    示例 1:

Perl

# 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

# Perl program to demonstrate the 
# (> or gt) And (< or lt)
# operator
   
# string scalar
$str = "geekforgeeks";
   
if($str < "GEEKSFORGEEKS")
{
    print "< doesn't work with string values!";
}
   
# comparing with capital string
if($str lt "GEEKSFORGEEKSZZZ")
{
    print"lt works with string values!";
}
   
# comparing with capital string
if($str gt "GEEKSFORGEEKSZZZ")
{
    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

# 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= "geeksforgeeks";
   
if (($str le "keeksforgeeks") and ($str ge "feeksforgeeks"))
{
    print "\nle and ge works!";
}

输出:

<= and>= works
le and ge works!

要记住的要点:

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

Perl

# 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”为真,但“17 gt 9”的结果为假。

Perl

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

输出:

True