Perl运算符/操作符用法完整指南

2021年4月2日12:32:34 发表评论 1,801 次浏览

运算符是任何编程语言的主要组成部分。运算符允许程序员对操作数执行不同种类的运算。在佩尔, 对于不同类型的操作数(例如标量和字符串), 运算符符号将有所不同。可以根据操作员的不同功能对其进行分类:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符
  • 三元运算符
  • 算术运算符

    这些用于对操作数执行算术/数学运算。

    加法:" +"运算符用于将两个操作数的值相加。例如:$a = 5; $b = 10;打印$a + $b;结果将是15

    减法:" –"运算符用于从左手操作数中减去右手操作数。例如:$a = 10; $b = 5;打印$a-$b;结果将是5

    乘法:" *"运算符用于将运算符两边的值相乘。例如:$a = 5; $b = 10;打印$a * $b;结果将是50

    除法运算符:当第一个操作数除以第二个操作数时, " /"运算符返回余数。例如:$a = 30; $b = 15;打印$a / $b;结果将是3

    模运算符:'%'运算符用于将左操作数与右操作数相除并返回余数。例如:$a = 10; $b = 15;打印$a%$b;结果将是5

    指数运算符:**运算符用于对操作数执行指数(幂)计算。例如:$a = 2; $b = 3;打印$a ** $b;结果将是8

    程序:演示算术运算

    # Perl Program to illustrate the Airthmetic Operators
      
    # Operands
    $a = 10;
    $b = 4;
      
    # using airthmetic 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" ;

    输出如下:

    Addition is: 14
    Subtraction is: 6
    Multiplication is: 40
    Division is: 2.5
    Modulus is: 2
    Exponent is: 10000

    关系运算符

    关系运算符用于比较两个值。这些运算符将返回1(true)或什么都不是(即0(false))。有时, 这些运算符也称为平等经营者。这些运算符使用不同的符号对字符串进行运算。要了解有关String的比较运算符操作, 可以参考这个.

    • 等于运算符:" =="检查两个值是否相等。如果等于则返回1, 否则不返回任何值。
    • 不等于运算符:"!="检查两个值是否相等。如果不相等, 则返回1, 否则不返回任何值。
    • 等于运算符的比较:" <=>"如果左操作数小于右操作数, 则返回-1, 如果等于, 则返回0, 否则返回1。
    • 大于运算符:">"如果left操作数大于right返回1, 否则不返回任何值。
    • 少于运算符:" <"如果左操作数小于右操作数则返​​回1, 否则不返回任何值。
    • 大于等于运算符:"> ="如果左操作数大于或等于右, 则返回1, 否则不返回任何值。
    • 小于等于运算符:" <="如果左操作数小于或等于右, 则返回1, 否则不返回任何值。

    程序:为了说明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

    逻辑运算符

    这些运算符用于组合两个或多个条件/约束或补充所考虑的原始条件的评估。

    • 逻辑与:‘和当同时考虑到两个条件时, ’运算符将返回true。例如, 当a和b都为真(即非零)时, $a和$b为真。你也可以使用&&.
    • 逻辑或:‘or当满足其中一个(或两个)条件时, ’运算符将返回true。例如, 如果a或b中的一个为真(即非零), 则$a或$b为真。当然, 当a和b均为true时, 结果为" true"。你也可以使用||
    • 逻辑非:如果满足考虑的条件, 则"非"运算符将给出1。例如, 如果$d为0, 则not($d)为true。

    程序:演示逻辑运算符的工作原理:

    # 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 Ouput will be 5
      
      Explanation:
      
      $a = 1 1 0 1
      $b = 0 1 0 1
      ---------------
      $c = 0 1  0 1
      ---------------
      
    • | (按位或)将两个数字作为操作数, 并对两个数字的每一位执行"或"运算。 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
      ---------------
      
    • ^(按位XOR)将两个数字作为操作数, 并对两个数字的每一位进行XOR。如果两个位不同, 则XOR的结果为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 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 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 "Subtractation 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
    Subtractation 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 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 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 staisfying 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

    木子山

    发表评论

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: