Perl 检测何时第二次执行else块

Perl 检测何时第二次执行else块,perl,Perl,我在if块中遇到问题。我在比较两个变量,如果它们相等,那么应该执行一些语句,否则应该执行其他语句。如果第二次执行false块,我需要返回新语句 例如: if($type eq $kind ){ $line1 .= "</p></list-item>\n<list-item><p>"; } else{ $line1 .= "\n<list list-type=\"$kind\">\n<list-item><p&g

我在if块中遇到问题。我在比较两个变量,如果它们相等,那么应该执行一些语句,否则应该执行其他语句。如果第二次执行false块,我需要返回新语句

例如:

if($type eq $kind ){
  $line1 .= "</p></list-item>\n<list-item><p>";
}
else{
  $line1 .= "\n<list list-type=\"$kind\">\n<list-item><p>";
}
这里type=bullet和kind=number,现在第二次执行else部分kind=number时,我想显示分配给$line的相同语句,我想显示like


在哪里再次检查条件?

您需要保持一些状态,并使用该状态确定打印第二个或第三个条件,或者。。。时间到了

my $has_printed_once = 0;

# your loop {

  if ($type eq $kind) {
    # no change
  } else {
    if ($has_printed_once == 0) {
      # print the second thing
    } else {
      $has_printed_once = 1;
      # print the first thing
    }
  }

# } close loop

在循环之外,可以定义状态变量

my $state = 1;
if($type eq $kind )
{
    $line1 .= "</p></list-item>\n<list-item><p>";
}
else
{
    if( $state eq 1 )
    {
        $line1 .= "\n<list list-type=\"$kind\">\n<list-item><p>";
        $state++ ;
    }
    else
    {
        $line1 .= "(whatever you want to write the second time)";
    }
}
在循环内部,测试并设置状态变量

my $state = 1;
if($type eq $kind )
{
    $line1 .= "</p></list-item>\n<list-item><p>";
}
else
{
    if( $state eq 1 )
    {
        $line1 .= "\n<list list-type=\"$kind\">\n<list-item><p>";
        $state++ ;
    }
    else
    {
        $line1 .= "(whatever you want to write the second time)";
    }
}
请注意,这是一段代码。我没有通过perl解释器运行它来检查错误。我希望它能给你这个想法