珀尔 |运算符 |套装 – 1
运算符是任何编程语言的主要构建块。运算符允许程序员对操作数执行不同类型的操作。在 Perl 中,对于不同类型的操作数(如标量和字符串),运算符符号会有所不同。运算符可以根据其不同的功能进行分类:
- 算术运算符
- 关系运算符
- 逻辑运算符
- 位运算符
- 赋值运算符
- 三元运算符
算术运算符
这些用于对操作数执行算术/数学运算。
- 加法: ' + '运算符用于将两个操作数的值相加。例如:
$a = 5;
$b = 10;
print $a + $b;
Here Result will be 15
- 减法: ' - '运算符用于从左手操作数中减去右手操作数。例如:
$a = 10;
$b = 5;
print $a - $b;
Here Result will be 5
- 乘法: ' * '运算符用于将运算运算符两边的值相乘。例如:
$a = 5;
$b = 10;
print $a * $b;
Here Result will be 50
- 除法运算符: ' / '运算符返回第一个操作数除以第二个操作数时的余数。例如:
$a = 30;
$b = 15;
print $a / $b;
Here Result will be 3
- 模运算符: “ % ”运算符用于将左操作数与右操作数相除并返回余数。例如:
$a = 10;
$b = 15;
print $a % $b;
Here Result will be 5
- 指数运算符: ' ** '运算符用于对操作数执行指数(幂)计算。例如:
$a = 2;
$b = 3;
print $a**$b;
Here Result will be 8
程序:演示算术运算运算符
Perl
# Perl Program to illustrate the Arithmetic Operators
# Operands
$a = 10;
$b = 4;
# using arithmetic operators
print "Addition is: ", $a + $b, "\n";
print "Subtraction is: ", $a - $b, "\n" ;
print "Multiplication is: ", $a * $b, "\n";
print "Division is: ", $a / $b, "\n";
print "Modulus is: ", $a % $b, "\n";
print "Exponent is: ", $a ** $b, "\n";
Perl
# Perl Program to illustrate the Relational Operators
# Operands
$a = 10;
$b = 60;
# using Relational Operators
if ($a == $b)
{
print "Equal To Operator is True\n";
}
else
{
print "Equal To Operator is False\n";
}
if ($a != $b)
{
print "Not Equal To Operator is True\n";
}
else
{
print "Not Equal To Operator is False\n";
}
if ($a > $b)
{
print "Greater Than Operator is True\n";
}
else
{
print "Greater Than Operator is False\n";
}
if ($a < $b)
{
print "Less Than Operator is True\n";
}
else
{
print "Less Than Operator is False\n";
}
if ($a >= $b)
{
print "Greater Than Equal To Operator is True\n";
}
else
{
print "Greater Than Equal To Operator is False\n";
}
if ($a <= $b)
{
print "Less Than Equal To Operator is True\n";
}
else
{
print "Less Than Equal To Operator is False\n";
}
if ($a <=> $b)
{
print "Comparison of Operator is True\n";
}
else
{
print "Comparison of Operator is False\n";
}
Perl
# Perl Program to illustrate the Logical Operators
# Operands
$a = true;
$b = false;
# AND operator
$result = $a && $b;
print "AND Operator: ", $result,"\n";
# OR operator
$result = $a || $b;
print "OR Operator: ", $result,"\n";
# NOT operator
$d = 0;
$result = not($d);
print "NOT Operator: ", $result;
Perl
# Perl Program to illustrate the Bitwise operators
#!/usr/local/bin/perl
use integer;
# Operands
$a = 80;
$b = 2;
# Bitwise AND Operator
$result = $a & $b;
print "Bitwise AND: ", $result, "\n";
# Bitwise OR Operator
$result = $a | $b;
print "Bitwise OR: ", $result, "\n";
# Bitwise XOR Operator
$result = $a ^ $b;
print "Bitwise XOR: ", $result, "\n";
# Bitwise Complement Operator
$result = ~$a;
print "Bitwise Complement: ", $result, "\n";
# Bitwise Left Shift Operator
$result = $a << $b;
print "Bitwise Left Shift: ", $result, "\n";
# Bitwise Right Shift Operator
$result = $a >> $b;
print "Bitwise Right Shift: ", $result, "\n";
Perl
# Perl program to demonstrate the working
# of Assignment Operators
#!/usr/local/bin/perl
# taking two variables & using
# simple assignments operation
$a = 8;
$b = 5;
# using Assignment Operators
print "Addition Assignment Operator: ", $a += $b, "\n";
$a = 8;
$b = 4;
print "Subtraction Assignment Operator: ", $a -= $b, "\n" ;
$a = 8;
$b = 4;
print "Multiplication Assignment Operator: ", $a*=$b, "\n";
$a = 8;
$b = 4;
print "Division Assignment Operator: ",$a/=$b, "\n";
$a = 8;
$b = 5;
print "Modulo Assignment Operator: ", $a%=$b,"\n";
$a = 8;
$b = 4;
print "Exponent Assignment Operator: ", $a**=$b, "\n";
Perl
# Perl program to demonstrate the working
# of Ternary Operator
$x = 5;
$y = 10;
# To find which value is greater
# Using Ternary Operator
$result = $x > $y ? $x : $y;
# displaying the output
print "The Larger Number is: $result"
Perl
# Perl program to demonstrate the working
# of Ternary Operator by using expression
# as the condition
# here maximum value can be 100
$MAX_VALUE = 100;
# suppose user provide value
$user_value = 444;
# To find which whether user provided
# value is satisfying the maximum value
# or not by using Ternary Operator
$result = $user_value <= $MAX_VALUE ? $user_value : $MAX_VALUE;
# displaying the output
# Here it will be MAX_VALUE
print "$result"
输出:
Addition is: 14
Subtraction is: 6
Multiplication is: 40
Division is: 2.5
Modulus is: 2
Exponent is: 10000
关系运算符
关系运算符用于比较两个值。这些运算符将返回 1 (true) 或 nothing(即 0(false)) 。有时这些运算符也称为Equality Operators 。这些运算符具有不同的符号来对字符串进行操作。要了解 String 上的比较运算符操作,您可以参考this 。
- 等于运算符:'=='检查两个值是否相等。如果等于则返回 1,否则不返回任何内容。
- 不等于运算符:'!='检查两个值是否相等。如果不相等则返回 1 否则不返回任何内容。
- 比较等于运算符:'< = >'如果左操作数小于右操作数则返回 -1,如果相等则返回 0,否则返回 1。
- 大于运算符:'>'如果左操作数大于右返回 1,否则不返回任何内容。
- 小于运算符:'<'如果左操作数小于右返回 1,否则不返回任何内容。
- 大于等于运算符:'>='如果左操作数大于或等于右返回 1,否则不返回任何内容。
- 小于等于运算符:'<='如果左操作数小于或等于右返回 1,否则不返回任何内容。
程序:说明 Perl 中的关系运算符
Perl
# Perl Program to illustrate the Relational Operators
# Operands
$a = 10;
$b = 60;
# using Relational Operators
if ($a == $b)
{
print "Equal To Operator is True\n";
}
else
{
print "Equal To Operator is False\n";
}
if ($a != $b)
{
print "Not Equal To Operator is True\n";
}
else
{
print "Not Equal To Operator is False\n";
}
if ($a > $b)
{
print "Greater Than Operator is True\n";
}
else
{
print "Greater Than Operator is False\n";
}
if ($a < $b)
{
print "Less Than Operator is True\n";
}
else
{
print "Less Than Operator is False\n";
}
if ($a >= $b)
{
print "Greater Than Equal To Operator is True\n";
}
else
{
print "Greater Than Equal To Operator is False\n";
}
if ($a <= $b)
{
print "Less Than Equal To Operator is True\n";
}
else
{
print "Less Than Equal To Operator is False\n";
}
if ($a <=> $b)
{
print "Comparison of Operator is True\n";
}
else
{
print "Comparison of Operator is False\n";
}
输出:
Equal To Operator is False
Not Equal To Operator is True
Greater Than Operator is False
Less Than Operator is True
Greater Than Equal To Operator is False
Less Than Equal To Operator is True
Comparison of Operator is True
逻辑运算符
这些运算符用于组合两个或多个条件/约束或补充考虑的原始条件的评估。
- 逻辑与:当考虑的两个条件都满足时,“和”运算符返回真。例如,当 a 和 b 都为真(即非零)时,$a 和 $b 为真。您也可以使用&& 。
- 逻辑或:当考虑的一个(或两个)条件满足时,'或'运算符返回真。例如,如果 a 或 b 之一为真(即非零),则 $a 或 $b 为真。当然,当 a 和 b 都为真时,它会给出结果“真”。您也可以使用||
- 逻辑非:如果考虑的条件满足,“非”运算符将给出 1。例如,如果 $d 为 0,则 not($d) 为真。
程序:演示逻辑运算符的工作:
Perl
# Perl Program to illustrate the Logical Operators
# Operands
$a = true;
$b = false;
# AND operator
$result = $a && $b;
print "AND Operator: ", $result,"\n";
# OR operator
$result = $a || $b;
print "OR Operator: ", $result,"\n";
# NOT operator
$d = 0;
$result = not($d);
print "NOT Operator: ", $result;
输出:
AND Operator: false
OR Operator: true
NOT Operator: 1
位运算符
这些运算符用于执行按位运算。它将首先将数字转换为位并对操作数执行位级操作。
- & (按位与)将两个数字作为操作数,并对两个数字的每一位进行与。仅当两个位都为 1 时,AND 的结果才为 1。例如
$a = 13; // 1101
$b = 5; // 0101
$c = $b & $a;
print $c;
Here Output will be 5
Explanation:
$a = 1 1 0 1
$b = 0 1 0 1
---------------
$c = 0 1 0 1
---------------
- | (按位或)将两个数字作为操作数,并对两个数字的每一位进行 OR。 OR 的结果为 1 两位中的任何一位为 1。例如
$a = 13; // 1101
$b = 5; // 0101
$c = $b | $a;
print $c;
Here Output will be 13
Explanation:
$a = 1 1 0 1
$b = 0 1 0 1
---------------
$c = 1 1 0 1
---------------
- ^(按位异或)将两个数字作为操作数,并对两个数字的每一位进行异或。如果两个位不同,则异或的结果为 1。例如
$a = 13; // 1101
$b = 5; // 0101
$c = $b ^ $a;
print $c;
Here Output will be 9
Explanation:
$a = 1 1 0 1
$b = 0 1 0 1
-------------
$c = 1 0 0 1
-------------
- ~(补码运算符)这是一元运算运算符,用作翻转位。由于有符号二进制数,它的工作是反转位并使用 2 的补码形式给出结果。
- (<<) 二进制左移运算符将取两个数字,左移第一个操作数的位,第二个操作数决定要移位的位数。它执行左操作数乘以右操作数指定的次数。例如:
$a = 60;
$c = $a << 2;
print $c;
Output: 240
Explanation:
60 * 2 = 120 ---(1)
120 * 2 = 240 ---(2)
- (>>)二进制右移运算符将取两个数字,右移第一个操作数的位,第二个操作数决定要移位的位数。它执行左操作数除以右操作数指定的次数。例如:
$a = 60;
$c = $a >> 2;
print $c;
Output: 15
Explanation:
60 / 2 = 30 ---(1)
30 / 2 = 15 ---(2)
程序:演示按位运算运算符的工作:
Perl
# Perl Program to illustrate the Bitwise operators
#!/usr/local/bin/perl
use integer;
# Operands
$a = 80;
$b = 2;
# Bitwise AND Operator
$result = $a & $b;
print "Bitwise AND: ", $result, "\n";
# Bitwise OR Operator
$result = $a | $b;
print "Bitwise OR: ", $result, "\n";
# Bitwise XOR Operator
$result = $a ^ $b;
print "Bitwise XOR: ", $result, "\n";
# Bitwise Complement Operator
$result = ~$a;
print "Bitwise Complement: ", $result, "\n";
# Bitwise Left Shift Operator
$result = $a << $b;
print "Bitwise Left Shift: ", $result, "\n";
# Bitwise Right Shift Operator
$result = $a >> $b;
print "Bitwise Right Shift: ", $result, "\n";
输出:
Bitwise AND: 0
Bitwise OR: 82
Bitwise XOR: 82
Bitwise Complement: -81
Bitwise Left Shift: 320
Bitwise Right Shift: 20
赋值运算符
赋值运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符符的右侧操作数是一个值。
不同类型的赋值运算符如下所示:
- “=”(简单赋值) :这是最简单的赋值运算符。该运算符用于将右侧的值赋给左侧的变量。
例子 :
$a = 10;
$b = 20;
- “+=”(添加赋值) :这个运算符是 '+' 和 '='运算符的组合。该运算符首先将左侧变量的当前值与右侧的值相加,然后将结果赋给左侧的变量。
例子 :
($a += $b) can be written as ($a = $a + $b)
- 如果最初存储在 a 中的值为 5。那么 ($a += 6) = 11。
- “-=”(减法赋值) :这个运算符是'-'和'='运算符的组合。该运算符首先从右侧的值中减去左侧变量的当前值,然后将结果分配给左侧的变量。
例子 :
($a -= $b) can be written as ($a = $a - $b)
- 如果最初存储在 a 中的值为 8。那么 ($a -= 6) = 2。
- “*=”(乘法赋值) :该运算符是 '*' 和 '='运算符的组合。该运算符首先将左侧变量的当前值乘以右侧的值,然后将结果分配给左侧的变量。
例子 :
($a *= $b) can be written as ($a = $a * $b)
- 如果最初存储在 a 中的值为 5。那么 ($a *= 6) = 30。
- “/=”(除法赋值) :这个运算符是'/'和'='运算符的组合。该运算符首先将左侧变量的当前值除以右侧的值,然后将结果分配给左侧的变量。
例子 :
($a /= $b) can be written as ($a = $a / $b)
- 如果最初存储在 a 中的值为 6。那么 ($a /= 2) = 3。
- “%=”(模数赋值) :这个运算符是 '%' 和 '='运算符的组合。该运算符首先将左侧变量的当前值与右侧的值取模,然后将结果分配给左侧的变量。
例子 :
($a %= $b) can be written as ($a = $a % $b)
- 如果最初存储在 a 中的值为 6。那么 ($a %= 2) = 0。
- “**=”(指数赋值) :该运算符是 '**' 和 '='运算符的组合。该运算符首先将左侧变量的当前值乘以右侧的值,然后将结果分配给左侧的变量。
例子 :
($a **= $b) can be written as ($a = $a ** $b)
- 如果最初,存储在 a 中的值为 6。那么 ($a **= 2) = 36。
程序:演示赋值运算符的工作
Perl
# Perl program to demonstrate the working
# of Assignment Operators
#!/usr/local/bin/perl
# taking two variables & using
# simple assignments operation
$a = 8;
$b = 5;
# using Assignment Operators
print "Addition Assignment Operator: ", $a += $b, "\n";
$a = 8;
$b = 4;
print "Subtraction Assignment Operator: ", $a -= $b, "\n" ;
$a = 8;
$b = 4;
print "Multiplication Assignment Operator: ", $a*=$b, "\n";
$a = 8;
$b = 4;
print "Division Assignment Operator: ",$a/=$b, "\n";
$a = 8;
$b = 5;
print "Modulo Assignment Operator: ", $a%=$b,"\n";
$a = 8;
$b = 4;
print "Exponent Assignment Operator: ", $a**=$b, "\n";
输出:
Addition Assignment Operator: 13
Subtraction Assignment Operator: 4
Multiplication Assignment Operator: 32
Division Assignment Operator: 2
Modulo Assignment Operator: 3
Exponent Assignment Operator: 4096
三元运算符
它是一个条件运算符,是 if-else 语句的简写版本。它有三个操作数,因此得名三元。它将根据布尔表达式的值返回两个值之一。
句法:
condition ? first_expression : second_expression;
解释:
condition: It must be evaluated to true or false.
If the condition is true
first_expression is evaluated and becomes the result.
If the condition is false,
second_expression is evaluated and becomes the result.
例子:
Perl
# Perl program to demonstrate the working
# of Ternary Operator
$x = 5;
$y = 10;
# To find which value is greater
# Using Ternary Operator
$result = $x > $y ? $x : $y;
# displaying the output
print "The Larger Number is: $result"
输出:
The Larger Number is: 10
注意:在三元运算符中,条件可以是任何表达式,也可以使用不同的运算符符,如关系运算符、逻辑运算符等。
例子:
Perl
# Perl program to demonstrate the working
# of Ternary Operator by using expression
# as the condition
# here maximum value can be 100
$MAX_VALUE = 100;
# suppose user provide value
$user_value = 444;
# To find which whether user provided
# value is satisfying the maximum value
# or not by using Ternary Operator
$result = $user_value <= $MAX_VALUE ? $user_value : $MAX_VALUE;
# displaying the output
# Here it will be MAX_VALUE
print "$result"
输出:
100