PERL5 Chapter III operator

Source: Internet
Author: User
Tags arithmetic operators bitwise operators alphanumeric characters


Chapter Three operator

by Flamephoenix

First, arithmetic operators
Second, integer comparison operators
Three, string comparison operators
Four, logical operators
Five, bitwise operators
Six, assignment operators
Vii. self-increment self-subtraction operator
Eight, string joins and repeating operators
Nine, comma operator
Ten, conditional operators
Xi. the order of the operators

One, arithmetic operators: + (plus),-(minus), * (multiply),/(except), * * (UP),% (residual),-(Monocular negative)
(1) The base of the exponentiation can not be negative, such as ( -5) * * 2.5 # error;
(2) The result of a exponentiation cannot exceed the limit indicated by the computer, such as the 999999 # error
(3) The operand of the remainder, if not an integer, is rounded to an integer and the operator to the right cannot be zero
(4) Monocular negative can be used for variables:-$y; # equivalent to $y *-1
Second, integer comparison operators

Table 3.1. Integer comparison operators

operator description
< less than
> greater than
equals
less than or equal to
greater than or equal to
Not equal to
<=> comparison, return 1,, or-1


The operators <=> results are:
0-Two values equal
1-Large first value
1-Large second value
Three, string comparison operators

Table 3.2. string comparison operators

Operator Describe  
Lt Less than  
Gt Greater than  
eq Equals  
Le Less than or equal  
Ge Greater than or equal  
Ne Not equal to  
Cmp Compare, return 1, 0, or-1  


Four, logical operators
Logic or: $a | | $b or $a or $b
Logic with: $a && $b or $a and $b
Logical non-:! $a or not $a
Logical XOR: $a XOR $b
Five, bitwise operators
Bits and:&
Bit or: |
Bit non: ~
Bit XOR: ^
Shift Left: $x << 1
Shift Right: $x >> 2
Note: Do not use & for negative integers, because Perl will convert them to unsigned numbers.
Six, assignment operators

Table 3.3. Assignment operator

Operator Describe
= Assignment only
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and Assignment
%= Remainder and assignment
**= Exponentiation and Assignment
&= Bitwise and and assignment
|= Bitwise OR and Assignment
^= Bitwise XOR and Assignment

Table 3.4. An example of a value assignment operator

An expression An equivalent expression
$a = 1; None (Basic Assignment)
$a-= 1; $a = $a-1;
$a *= 2; $a = $a * 2;
$a/= 2; $a = $a/2;
$a%= 2; $a = $a% 2;
$a **= 2; $a = $a * * 2;
$a &= 2; $a = $a & 2;
$a |= 2; $a = $a | 2;
$a ^= 2; $a = $a ^ 2;


. = can occur multiple times in an assignment statement, such as:
$value 1 = $value 2 = "a string";
. = as a sub-expression
($a = $b) + = 3;
Equivalent to
$a = $b;
$a + = 3;
However, it is not recommended to use this method.
Vii. self-increment decrement operator: + + 、--(same as usage in C + +)
Do not use this operator on both sides of the variable: + + $var--# Error
Do not use again in the same expression after the variable has been self-increment/subtract: $var 2 = $var 1 + + + $var 1; # error
. in Perl, + + can be used for strings, but when the trailing character is ' z ', ' z ', ' 9 ', such as:
$stringvar = "ABC";
$stringvar + +; # $stringvar contains "Abd" now

$stringvar = "ABC";
$stringvar + +; # $stringvar contains "ABD" now

$stringvar = "Abz";
$stringvar + +; # $stringvar now contains "ACA"

$stringvar = "Agzzz";
$stringvar + +; # $stringvar now contains "AHAAA"

$stringvar = "AB4";
$stringvar + +; # $stringvar now contains "AB5"

$stringvar = "bc999";
$stringvar + +; # $stringvar now contains "bd000"
Do not use--,perl to convert a string to a number and then self-subtract.
$stringvar = "ABC";
$stringvar--; # $stringvar =-1 now

If the string contains non-alphanumeric characters, or the numbers are in letters, the value before the + + operation is converted to a number zero, so the result is 1, such as:
$stringvar = "Ab*c";
$stringvar + +;
$stringvar = "ab5c";
$stringvar + +;
Eight, string joins and repeating operators
Join:.
Repeat: X
Join and assign value (similar to + =):. =
Cases:
$newstring = "Potato". "Head";
$newstring = "T" x 5;
$a = "be";
$a. = "Witched"; # $a is now "Bewitched"
Nine, comma operator
The preceding expression is preceded by an operation, such as:
$var 1 + = 1, $var 2 = $var 1;
Equivalent to
$var 1 + = 1;
$var 2 = $var 1;
The only reason to use this operator is to improve the readability of the program, combining two closely related expressions, such as:
$val = 26;
$result = (+ + $val, $val + 5); # $result = 32
Note If there are no parentheses here, the meaning is different:
$val = 26;
$result = + + $val, $val + 5; # $result = 27
Ten, conditional operators
Similar to C, condition? value 1: Value 2, when the condition is true, take value 1, take value 2 when false, for example:
$result = $var = = 0? 14:7;
$result = + ($divisor = = = 0? 0: $dividend/$divisor);
In PERL 5, you can also use the conditional operator on the left side of an assignment to select the variable that is assigned, such as:
$condvar = = 43? $var 1: $var 2 = 14;
$condvar = = 43? $var 1 =: $var 2 = 14;
Xi. the order of the operators

Table 3.6. Order of Operators

Operator Describe
++, -- Self-increment, self-reduction
-, ~, ! Monocular
** Powers
=~, !~ Pattern matching
*, /, %, x Multiply, divide, withdraw, repeat
+, -, . Add, subtract, join
<<, >> Shift
- e,- R, etc. File status
<, <=, >, >=, lt,le, gt, ge Unequal comparison
= =,! =, <=>, eq,ne, cmp Equality comparison
& Bit and
|, ^ Bit or, bit XOR or
&& Logic and
|| Logical OR
.. List range
? and : Conditional operator
=, +=, -=, *=, Assign value
And so on  
, Comma operator
Not Low-precedence Logical Not
and Low-precedence Logical AND
or, xor Low-precedence logical OR and XOR

. Operator Associativity (associativity):

Table 3.7. Operator associativity

Operator Binding nature
++, -- No
-, ~, ! Right-to-left
** Right-to-left
=~, !~ Left-to-right
*, /, %, x Left-to-right
+, -, . Left-to-right
<<, >> Left-to-right
- e,- R, No
<, <=, >, >=, lt, le, gt,ge Left-to-right
= =,! =, <=>, eq, ne, cmp Left-to-right
& Left-to-right
|, ^ Left-to-right
&& Left-to-right
|| Left-to-right
.. Left-to-right
? and : Right-to-left
=, +=, -=, *=, Right-to-left
And so on  
, Left-to-right
Not Left-to-right
and Left-to-right
or, xor Left-to-right



Suggestions:
1, when you are not sure whether an operator is executed first, be sure to use parentheses to clear it.
2, use multirow, space and other ways to improve the readability of the program.

Previous chapter Next Chapter catalogue

PERL5 Chapter III operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.