perl嵌套if或其他东西

时间:2012-09-12 06:13:15

标签: perl if-statement

我使用了简单的嵌套if语句。

要求如下。

if (Condition1) {
    if (Condition2) {
        print "All OK";
    }
    else {
        print "Condition1 is true but condition2 not";
    }
    else {print "Condition1 not true";
}

是否可以在perl中编写此代码,或者是否有其他简短/更好的方法来满足此条件?

6 个答案:

答案 0 :(得分:2)

TIMTOWTDIàlaternary operator

print $condition1
      ? $condition2
        ? "All OK\n"
        : "Condition 1 true, Condition 2 false\n"
      :   "Condition 1 false\n";

答案 1 :(得分:2)

if条件1为true子句缺少其应该在最后一个之前插入的结束}

尝试以这种方式排列:

if (...) {
    if (...) {
        ...
    }
    else {
        ...
    }
}
else {
    ....
}

答案 2 :(得分:1)

如果您的Perl版本> = 5.10。

,则可以使用给定的..
use v5.14;

my $condition1 = 'true';
my $condition2 = 'True';

given($condition1) {
    when (/^true$/) {
        given($condition2) {
            when (/^True$/) { say "condition 2 is True"; }
            default         { say "condition 2 is not True"; }
        }
    }
    default { say "condition 1 is not true"; }
}

答案 3 :(得分:1)

怎么样:

if (Condition1=false) {
     print "Condition1 not true";
}
elsif (Condition2=True ) {
    print "All OK"; 
}
else {
    print "Condition1 is true but condition2 not";  
}

答案 4 :(得分:0)

if (Condition1) {
    if (Condition2 ) {
        print "Both true";
    }
    else {
        print "Condition1 is true, Condition2 false";
    }
}
else {
    if (Condition2 ) {
        print "Condition1 false Condition2 is true";
    }
    else {
       print "Both false";
    }
}

答案 5 :(得分:0)

#OR condition
if ( ($file =~ /string/) || ($file =~ /string/) ){
}

#AND condition
if ( ($file =~ /string/) && ($file =~ /string/) ){
}
相关问题