The Perl Tutorial: Operators (3)

xiaoxiao2021-03-06  43

Operators (operator)

Other computer languages ​​have operators, Perl is also supported. The operator is used to calculate the operand, in all computer languages, the operator of Perl has a certain advantage.

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.

In front, we already know the assignment operator (=). This operator is to assign the value of a variable to another variable:

We've alleady encountered the assignment operator (=). This Operator Assigns a value from one variable to another:

$ String1 = $ String2;

The above code assigns $ String2's value to $ String1.

Now let's take a look at the binary and one yuan arithmetic operator:

The Above Code Assigns The Value of $ String2 To $ String1. We will now consider binary and unary ferrest.

OP1 OP2 Do two values ​​to add OP1 - OP2 to two numerical assumptions OP1 * OP2 Make multiplication operation OP1 / OP2 Multiply OP1 / OP2 Doing OP1% OP2 for two integer values The remaining OP1 XX OP2 Ask Operation: Ask OP1 OP2 Power OP1 Pre-plus Action: The value of the OP1 first increases, then assigns the value to its own OP1 , add operation: OP1 value first assign itself, add value - -OP1 Preferred operation: The value of OP1 is decreased, then assigen a value to its own OP1- and reduced operation: OP1 value is given to itself, then reduce the value

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 The logical operator is mainly used to control the runtime process of the program. The logical operators supported by Perl are:

The Logical Operators Are Used Mainly To Control The Flow of The Program. Some of The Logical Operators That Perl Supports include:

&& and operators: only and when both values ​​are TRUE, the return value is true || or operator: only and when at least one value is TRUE, the return value is True! Non-operator: Take a value The reverse op1 == OP2 detects whether the two values ​​are equal to OP1! = OP2 detects whether the two values ​​are not equal to OP1 OP2 If the OP1 is greater than OP2, the return value is TRUE OP1 <= OP2 If the OP1 is less than or equal to OP2, the return value is true op1> = OP2 If the OP1 is greater than or equal to OP2, the return value is true op1 <=> OP2 If the OP1 is less than OP2, the return value is -1; if they are equal, the return value is 0; if OP2 is greater than OP1, and the return value is 1. OP1 EQ OP2 If the two strings are equal, the return value is true op1 Ne OP2 If the two strings are not equal, the return value is True OP1 LT OP2 If the string OP1 is smaller than the string OP2, the return value is True Op1 le op2 if character The string OP1 is less than or equal to the string OP2, the return value is true OP1 GT OP2 If the string OP1 is larger than the string OP2, the return value is true op1 GE OP2 If the string OP1 is greater than or equal to the string OP2, the return value is TRUE OP1 CMP OP2 In the face of the description of <=>, the string operation is

&& 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 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 Numeric Operator Will Return -1 IF OP1 IS LESS THAN OP2, IT WILL RETURN 0 IF THEN 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 O perator 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. You can also use the following linkage operators, connect strings:

There Are Other Operators Which Can Also Be Used In Perl. We Have The ConcateNation Operator for strings:

$ String1. $ String2

The above code will connect the string $ String1 with $ String2 to form a new string. The Above Code Will Concatenate $ String1 With $ String2 To Form A New String.

We can also specify a string several times with a cyclic operator (x):

We Also Have The Repetition Operator (x) This Operator Will Repeat A String a Certain Number of Times Specified:

$ String1 x 2;

The above code will repeat the string $ String1 twice.

The Above Code Will Repeat $ String1 TWICE.

We can also specify a range of arrays with the following operations:

The Range Operator Allows US To Use Ranges, in Arrays Or Patters:

@ array = (1..50);

This will allocate 50 elements to an array.

The Above Code Will Assign 50 Elements to the array.

We know that many operators above are operated for scalar values, but if we want to operate the scalar values ​​saved in an array, we can use the "array slice", for example, there are 10 values, if We want to assign the value 5 and value 6 in the array to two scales, we can do this:

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]; # array subscript from 0 #Remember That Arrays Start At Subscript 0.

The above code We divide a comma in the subscript in the composite subscript behind the array name, the subscript specifies a plurality of values ​​in an array (here two values), and assign it to the left. (Here is two scales).

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

转载请注明原文地址:https://www.9cbs.com/read-86930.html

New Post(0)