Show Example 3
优质
小牛编辑
129浏览
2023-12-01
假设变量$ a保持10,变量$ b保持20,然后下面是Perl中可用的赋值运算符及其用法 -
Sr.No. | 操作符和说明 |
---|---|
1 | = 简单赋值运算符,将右侧操作数的值分配给左侧操作数 Example - $ c = $ a + $ b将$ a + $ b的值分配给$ c |
2 | += 添加AND赋值运算符,它将右操作数添加到左操作数并将结果赋给左操作数 Example - $ c + = $ a相当于$ c = $ c + $ a |
3 | -= 减去AND赋值运算符,它从左操作数中减去右操作数,并将结果赋给左操作数 Example - $ c - = $ a相当于$ c = $ c - $ a |
4 | *= 乘以AND赋值运算符,它将右操作数与左操作数相乘,并将结果赋值给左操作数 Example - $ c * = $ a相当于$ c = $ c * $ a |
5 | /= 除法AND赋值运算符,它将左操作数除以右操作数,并将结果赋值给左操作数 Example - $ c/= $ a相当于$ c = $ c/$ a |
6 | %= 模数AND赋值运算符,使用两个操作数获取模数,并将结果赋给左操作数 Example - $ c%= $ a相当于$ c = $ c%a |
7 | **= 指数AND赋值运算符,对运算符执行指数(幂)计算并将值赋给左操作数 Example - $ c ** = $ a相当于$ c = $ c ** $ a |
例子 (Example)
尝试以下示例以了解Perl中可用的所有赋值运算符。 将以下Perl程序复制并粘贴到test.pl文件中并执行该程序。
#!/usr/local/bin/perl
$a = 10;
$b = 20;
print "Value of \$a = $a and value of \$b = $b\n";
$c = $a + $b;
print "After assignment value of \$c = $c\n";
$c += $a;
print "Value of \$c = $c after statement \$c += \$a\n";
$c -= $a;
print "Value of \$c = $c after statement \$c -= \$a\n";
$c *= $a;
print "Value of \$c = $c after statement \$c *= \$a\n";
$c /= $a;
print "Value of \$c = $c after statement \$c /= \$a\n";
$c %= $a;
print "Value of \$c = $c after statement \$c %= \$a\n";
$c = 2;
$a = 4;
print "Value of \$a = $a and value of \$c = $c\n";
$c **= $a;
print "Value of \$c = $c after statement \$c **= \$a\n";
执行上述代码时,会产生以下结果 -
Value of $a = 10 and value of $b = 20
After assignment value of $c = 30
Value of $c = 40 after statement $c += $a
Value of $c = 30 after statement $c -= $a
Value of $c = 300 after statement $c *= $a
Value of $c = 30 after statement $c /= $a
Value of $c = 0 after statement $c %= $a
Value of $a = 4 and value of $c = 2
Value of $c = 16 after statement $c **= $a