在Perl中使用AND运算符测试两个布尔值

quhf5bfb  于 2022-11-15  发布在  Perl
关注(0)|答案(2)|浏览(184)

我想在Perl中测试两个布尔值。如果都是0,则执行x,如果都不是,则执行y。例如:$var1 = 0;在Perl中为假,$var2 = 0; Perl中为false

if($var1==0 && var2==0)
{
  x //both are false
}
else
{
 y //both are true
}
2w3kk1z5

2w3kk1z51#

if ( $var1 && $var2 ) {
   # Both are true.
}
elsif ( !$var1 && !$var2 ) {
   # Both are false.
}

if ( !( $var1 xor $var2 ) ) {
   # Both are true or both are false.
   if ( $var1 ) {
      # Both are true.
   } else {
      # Both are false.
   }
}

if ( $var1 ) { 
   if ( $var2 ) {
      # Both are true.
   }
} else {
   if ( !$var2 ) {
      # Both are false.
   }
}
zwghvu4y

zwghvu4y2#

因此...假设只有在$var1和$var2都为真时才需要y:

if (!$var1 and !$var2) {
    # do x
  } elsif ($var1 and $var2) {
     # do y
  }

但是TIMTOWTDI。因为Perl。

相关问题