2

単純なネストされたifステートメントを打ち込みました。

要件は以下の通りです。

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

このコードを Perl で書くことは可能ですか? または、この条件を満たすための別の短い方法またはより良い方法はありますか?

4

6 に答える 6

2

三項演算子TIMTOWTDI :

print $condition1
      ? $condition2
        ? "All OK\n"
        : "Condition 1 true, Condition 2 false\n"
      :   "Condition 1 false\n";
于 2012-09-12T06:35:05.650 に答える
2

if 条件 1が true です。句には、最後のelse}の直前に挿入する必要がある終了がありません。

このように物事を並べてみてください:

if (...) {
    if (...) {
        ...
    }
    else {
        ...
    }
}
else {
    ....
}
于 2012-09-12T06:36:02.123 に答える
1

お使いのPerlのバージョンが5.10以上の場合はgive..whenを使用できます。

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"; }
}
于 2012-09-12T06:32:15.163 に答える
1

どうですか:

if (Condition1=false) {
     print "Condition1 not true";
}
elsif (Condition2=True ) {
    print "All OK"; 
}
else {
    print "Condition1 is true but condition2 not";  
}
于 2012-09-12T07:47:26.770 に答える
0
if (!Condition1) {
  print "Condition1 not true";
}
else {
  if (Condition2) {
    print "All OK";
  }
  else {
    print "Condition1 is true but condition2 not";
  }
}
于 2012-09-12T12:47:40.110 に答える
0
#OR condition
if ( ($file =~ /string/) || ($file =~ /string/) ){
}

#AND condition
if ( ($file =~ /string/) && ($file =~ /string/) ){
}
于 2013-08-01T08:07:12.883 に答える