Perl-在foreach循环中跳过第二个if

Perl-在foreach循环中跳过第二个if,perl,if-statement,foreach,Perl,If Statement,Foreach,如果在Perl foreach循环中有2个If循环,那么如果脚本输入了第一个If,如何跳过第二个If循环 例如: my $var1 = 1; my $var2 = 1; for my $item ('...') { if ( $var1 == 1 ) { print "First IF \n"; } if ( $var2 == 1 ) { print "Second IF (I don't want this to be printed

如果在Perl foreach循环中有2个If循环,那么如果脚本输入了第一个If,如何跳过第二个If循环

例如:

my $var1 = 1;
my $var2 = 1;

for my $item ('...') {
    if ( $var1 == 1 ) {
        print "First IF \n";
    }
    if ( $var2 == 1 ) {
        print "Second IF (I don't want this to be printed if the first if is true)\n";
    }
}
带有:


如果谓词1的计算结果为true,则不会考虑谓词2及其关联的块[NB:not loop]。一种解决方案是设置逻辑,以便仅在测试1为false时检查测试2

foreach my $item (@items) {
  if (test1) {
    ...
  elsif (test2) {
    ...
  }
}
但另一种选择是使用next直接跳到循环的下一个迭代

foreach my $item (@items) {
  if (test1) {
    ...
    next;
  }
  if (test2) {
    ...
  }
}

@Zeldalf NB:如果$a==1-不仅仅是a@mpapec你在挑剔我的压痕?!那些。。。我使用的每一种C-括号语言都是这样缩进的,我不会因为一位作者这么说就把它改成Perl!请避免使用$a和$b变量,它们用于排序函数。另外,在表示变量时最好避免使用裸字
foreach my $item (@items) {
  if (test1) {
    ...
    next;
  }
  if (test2) {
    ...
  }
}