perl入门_Perl中的值比较入门指南

perl入门

Perl comparison operators can sometimes be confusing to new Perl programmers. The confusion stems from the fact that Perl actually has two sets of comparison operators - one for comparing numeric values and one for comparing string American Standard Code for Information Interchange (ASCII) values. 

Perl比较运算符有时会使新的Perl程序员感到困惑。 造成混淆的原因是Perl实际上有两组比较运算符-一组用于比较数字值,一组用于比较字符串美国信息交换标准码(ASCII)值。

Since comparison operators are typically used to control logical program flow and make important decisions, using the wrong operator for the value you are testing can lead to bizarre errors and hours of debugging, if you're not careful.

由于比较运算符通常用于控制逻辑程序流并做出重要的决定,因此,如果不小心,对测试值使用错误的运算符可能会导致异常错误和调试时间。

Don't forget to catch what's written at the very bottom of this page for some last-minute things to remember.

不要忘了抓住本页最后写的内容,以记住一些最新的内容。

平等,不平等 ( Equal, Not Equal )

The simplest and probably most used comparison operators test to see if one value is equal to another value. If the values are equal, the test returns true, and if the values are not equal, the test returns false.

最简单且可能使用最多的比较运算符进行测试,以查看一个值是否等于另一个值。 如果值相等,则测试返回true,如果值不相等,则测试返回false。

For testing the equality of two numeric values, we use the comparison operator ==. For testing the equality of two string values, we use the comparison operator eq (EQual).

为了测试两个数值是否相等,我们使用比较运算符== 。 为了测试两个字符串值的相等性,我们使用比较运算符eq (EQual)。

Here's an example of both:

这是两个示例:


if (5 == 5) { print "== for numeric


if ('moe' eq 'moe') { print "eq (EQual) for string

Testing for the opposite, not equal, is very similar. Remember that this test will return true if the values tested are not equal to each other. To see if two numeric values are not equal to each other, we use the comparison operator !=. To see if two string values are not equal to each other, we use the comparison operator ne (Not Equal).

相反的测试(不相等)非常相似。 请记住,如果测试的值彼此相等,则此测试将返回true 。 要查看两个数值是否彼此相等,我们使用比较运算符!= 。 要查看两个字符串值是否彼此相等,我们使用比较运算符ne (不等于)。


if (5 != 6) { print "!= for numeric


if ('moe' ne 'curly') { print "ne (Not Equal) for string

大于,大于或等于 ( Greater Than, Greater Than or Equal To )

Now let's look at the greater than comparison operators. Using this first operator, you can test to see if one value is greater than another value. To see if two numeric values are greater than each other, we use the comparison operator >. To see if two string values are greater than each other, we use the comparison operator gt (Greater Than).

现在让我们看一下大于比较运算符。 使用第一个运算符,可以测试一个值是否大于另一个值。 要查看两个数值是否彼此大于,我们使用比较运算符> 。 要查看两个字符串值是否彼此更大,我们使用比较运算符gt (大于)。


if (5 > 4) { print "> for numeric


if ('B' gt 'A') { print "gt (Greater Than) for string

You can also test for greater than or equal to, which looks very similar. Keep in mind that this test will return true if the values tested are equal to each other, or if the value on the left is greater than the value on the right.

您还可以测试大于或等于 ,看起来非常相似。 请记住,如果测试的值彼此相等,或者左侧的值大于右侧的值,则此测试将返回true

To see if two numeric values are greater than or equal to each other, we use the comparison operator >=. To see if two string values are greater than or equal to each other, we use the comparison operator ge (Greater-than Equal-to).

要查看两个数值是否大于或等于彼此,我们使用比较运算符> = 。 要查看两个字符串值是否大于或等于彼此,我们使用比较运算符ge (大于等于)。


if (5 >= 5) { print ">= for numeric


if ('B' ge 'A') { print "ge (Greater-than Equal-to) for string

少于,少于或等于 ( Less Than, Less Than or Equal To )

There are a variety of comparison operators you can use to determine the logical flow of your Perl programs. We've already discussed the difference between the Perl numeric comparison operators and the Perl string comparison operators, which can cause some confusion to new Perl programmers. We've also learned how to tell if two values are equal to, or not equal to each other, and we've learned how to tell if two values are greater than or equal to each other.

您可以使用多种比较运算符来确定Perl程序的逻辑流程。 我们已经讨论了Perl数字比较运算符和Perl字符串比较运算符之间的区别,这可能会给新的Perl程序员带来一些困惑。 我们还学习了如何区分两个值是否相等,并且还学会了如何分辨两个值是否彼此相等。

Let's look at the less than comparison operators. Using this first operator, you can test to see if one value is less than another value. To see if two numeric values are less than each other, we use the comparison operator <. To see if two string values are less than each other, we use the comparison operator lt (Less Than).

让我们看一下小于比较运算符。 使用第一个运算符,您可以测试一个值是否小于另一个值。 要查看两个数值是否彼此小于 ,我们使用比较运算符< 。 要查看两个字符串值是否彼此小于 ,我们使用比较运算符lt (Less Than)。


if (4 < 5) { print "< for numeric


if ('A' lt 'B') { print "lt (Less Than) for string

You can also test for, less than or equal to, which looks very similar. Remember that this test will return true if the values tested are equal to each other, or if the value on the left is less than the value on the right. To see if two numeric values are less than or equal to each other, we use the comparison operator <=. To see if two string values are less than or equal to each other, we use the comparison operator le (Less-than Equal-to).

您也可以测试小于或等于 ,看起来非常相似。 请记住,如果测试的值彼此相等,或者左侧的值小于右侧的值,则此测试将返回true 。 要查看两个数值是否小于或等于彼此,我们使用比较运算符<= 。 要查看两个字符串值是否彼此小于或等于 ,我们使用比较运算符le (小于等于)。


if (5 <= 5) { print "<= for numeric


if ('A' le 'B') { print "le (Less-than Equal-to) for string

有关比较运算符的更多信息 ( More Information on Comparison Operators )

When we talk about string values being equal to each other, we're referring to their ASCII values. So, the capital letters are technically less than the lowercase letters, and the higher the letter is in the alphabet, the higher the ASCII value.

当我们谈论字符串值彼此相等时,我们指的是它们的ASCII值。 因此,从技术上讲,大写字母小于小写字母,并且字母在字母表中越高,则ASCII值越高。

Make sure you check your ASCII values if you're trying to make logical decisions based on strings.

如果要基于字符串做出逻辑决策,请确保检查ASCII值。

翻译自: https://www.thoughtco.com/comparison-operators-compare-values-in-perl-2641145

perl入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值