其它计算机语言所具有的操作符 ,Perl 也同样支持。操作符用来对操作数进行运算,在所有计算机语言中, Perl 的操作符具有一定的优势。
Perl supports the same set of operators as most other computer languages. Operators allow a computer language to perform actions on operands. As in every computer language, Perl operators also have a certain precedence.
在前面,我们已经知道赋值操作符 (=) 。这个操作符作用是将一个变量的值赋给另一个变量:
We've already encountered the assignment operator (=). This operator assigns a value from one variable to another:
$string1 = $string2;
上面的代码将 $string2 的值赋给 $string1 。
现在我们来看一下二元和一元运算操作符:
The above code assigns the value of $string2 to $string1. We will now consider binary and unary arithmatic operators.
op1 + op2 对两个数值做加法操作 op1 - op2 对两个数值做减法操作 op1 * op2 对两个数值做乘法操作 op1 / op2 对两个数值做除法操作 op1 % op2 求两个整型数值的余数 op1 xx op2 求幂操作:求 op1 的 op2 次幂 ++op1 前加操作: op1 的值先增加,然后将值赋给自身 op1++ 后加操作: op1 的值先赋给自身,再增加值 --op1 前减操作: op1 的值先减少,然后将值赋给自身 op1— 后减操作: op1 的值先赋给自身,再减少值op1 + op2 The addition operator will add two numbers. op1 - op2 The subtraction operator will subtract two numbers. op1 * op2 The multiplication operator will multiply two numbers. op1 / op2 The division operator will divide two numbers. op1 % op2 The modulus operator will return the remainder of the division of two integer operands. op1 xx op2 The exponentiation operator will raise op1 to the power of op2. ++op1 The pre-increpment operator will increase the value of op1 first, then assign it. op1++ The post-increment operator will increase the value of op1 after it is assigned. --op1 The pre-decrement operator will decrease the value of op1 before it is assigned. op1-- The post-decrement operator will decrease the value of op1 after it is assigned.
逻辑操作符主要用于控制程序的运行流程。 Perl 所支持的逻辑操作符有:
The logical operators are used mainly to control the flow of the program. Some of the logical operators that Perl supports include:
&& 与操作符:仅且当两个值都为 true 时,返回值为 true || 或操作符:仅且当至少有一个值为 true 时,返回值为 true ! 非操作符:对一个值取反 op1 == op2 检测两个数值是否相等 op1 != op2 检测两个数值是否不相等 op1 < op2 如果 op1 小于 op2 ,返回值为 true op1 > op2 如果 op1 大于 op2 ,返回值为 true op1 <= op2 如果 op1 小于等于 op2 ,返回值为 true op1 >= op2 如果 op1 大于等于 op2 ,返回值为 true op1 <=> op2 如果 op1 小于 op2 ,返回值为 -1 ;如果它们相等,返回值为 0 ;如果 op2 大于 op1 ,返回值为 1 。 op1 eq op2 如果两个字符串相等,返回值为 true op1 ne op2 如果两个字符串不相等,返回值为 true op1 lt op2 如果字符串 op1 小于字符串 op2 ,返回值为 true op1 le op2 如果字符串 op1 小于等于字符串 op2 ,返回值为 true op1 gt op2 如果字符串 op1 大于字符串 op2 ,返回值为 true op1 ge op2 如果字符串 op1 大于等于字符串 op2 ,返回值为 true op1 cmp op2 见上面对 <=> 的描述,字符串操作与其相同 && The AND operator takes two values, and will return true only if both values are true. || The OR operator takes two values, and will return true only if at least one value is true. ! The NOT operator will negate a value. op1 == op2 The equals operator checks for the equality of two numerical values. op1 != op2 This not-equals operator checks for the inequality of two numerical values. op1 < op2 This numerical operator will return true if op1 is less than op2. op1 > op2 This numerical operator will return true if op1 is greater than op2. op1 <= op2 This numerical operator will return true if op1 is less than or equal to op2. op1 >= op2 This numerical operator will return true if op1 is greater than or equal to op2 op1 <=> op2 This numerical operator will return -1 if op1 is less than op2, it will return 0 if they are equal, and it will return 1 if op2 is greater than op1. op1 eq op2 This string operator will return true if the two strings are equal. op1 ne op2 This string operator will return true if both strings are not equal. op1 lt op2 This string operator will return true if op1 is less than op2. op1 le op2 This string operator will return true if op1 is less than or equal to op2. op1 gt op2 This string operator will return true if op1 is greater than op2. op1 ge op2 This string operator will return true if op1 is greater than or equal to op2. op1 cmp op2 This string operator functions in the same manner as the numerical <=> operator described above.在 Perl 中还可以用下面的连操操作符,连接字符串:
There are other operators which can also be used in Perl. We have the concatenation operator for strings (.):
$string1 . $string2
上面的代码将连接字符串 $string1 与 $string2 以形成一个新的字符串。
The above code will concatenate $string1 with $string2 to form a new string.
我们还可以用循环操作符 (x) 指定一个字符串重复几次:
We also have the repetition operator (x), this operator will repeat a string a certain number of times specified:
$string1 x 2;
上面的代码将重复字符串 $string1 两次。
The above code will repeat $string1 twice.
我们还可以用下面的操作指定一个数组的范围:
The range operator allows us to use ranges, in arrays or patters:
@ array = (1..50);
这将分配 50 个元素到数组。
The above code will assign 50 elements to the array.
我们知道上面的许多操作符都针对标量值进行操作,但是如果我们想操作数组中所保存的标量值进行操作,我们可以用“数组切片”操作,例如,有 10 个值的数组,如果我们想将数组中的数值 5 和数值 6 赋给两个标量,我们可以这样做:
As we have already seen, we have a wide variety of operators that work with scalar values, but if we wanted to work with arrays we could do what is called "array splicing". Let's say we have an array with 10 values. If we want to assign values 5 and 6 to two scalar values we could do the following:
($one,$two) = @array[4,5]; # 数组下标从 0 开始 #remember that arrays start at subscript 0.
上面的代码我们在数组名后面的综括号中的下标之间用逗号分隔,下标指定了数组中某一范围的多个值(这里是两个值),并将其赋给左边的标量(这里是两个标量)。
In the above code we just spliced two values from the array. When we do array splicing we use the @ sign, followed by the array name, followed by brackets and the subscripts that you want separated by commas. So instead of having two lines for the above code we have one. We need to enclose the two scalar values in parentheses in order to group them.
本文地址:http://com.8s8s.com/it/it31073.htm