加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在perl中打破do while循环

发布时间:2020-12-15 23:28:02 所属栏目:大数据 来源:网络整理
导读:我在perl中有一个while循环,如果在中间我需要爆发.我看到最后的标签;是我如何摆脱循环,但我无法弄清楚在哪里添加标签.这是我的示例代码. my $count = 5;do { print $count; if ( $count == 2 ) { last TAG; } $count--;} while ( $count 0 );TAG: print "out
我在perl中有一个while循环,如果在中间我需要爆发.我看到最后的标签;是我如何摆脱循环,但我无法弄清楚在哪里添加标签.这是我的示例代码.

my $count = 5;

do {
    print $count;

    if ( $count == 2 ) { last TAG; }

    $count--;

} while ( $count > 0 );

TAG: print "out";

以上失败,找不到标签.我究竟做错了什么?谢谢你的期待.

解决方法

不要使用 do的 statement modifier表格:

07001 BLOCK does not count as a loop,so the loop control statements 07003,07004,or 07005 cannot be used to leave or restart the block. See 07006 for alternative strategies.

相反,我建议使用while循环.

my $count = 5;

while ( $count > 0 ) {
    print $count;
    last if $count == 2;
    $count--;
}

print "out";

输出:

5432out

如果你需要一个在测试条件之前总是运行一次的构造,那么使用这个形式的while循环:

while (1) {
    ...

    last if COND;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读