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

perl中last的用法

发布时间:2020-12-16 00:11:53 所属栏目:大数据 来源:网络整理
导读:在C语言中,如果想要退出一个循环,我们可以使用break。在perl中,没有beak这个关键字,但是perl却也提供了另外一个关键字,让程序从循环中跳出,那就是last。现在我们就看一下last的使用。 #! /usr/bin/perl use strict; use warnings; my @array = (1 .. 1

在C语言中,如果想要退出一个循环,我们可以使用break。在perl中,没有beak这个关键字,但是perl却也提供了另外一个关键字,让程序从循环中跳出,那就是last。现在我们就看一下last的使用。

#! /usr/bin/perl
use strict;
use warnings;

my @array = (1 .. 100);
foreach my $elem (@array)
{
??? print "$elemt";
??? last if($elem / 3 == 0)
}
print "nHello!n";

上面的程序,在foreach中,只会打印三个数字1,2,3就退出了,就好比执行时遇到break一样。

注意:last不能用于具有返回值得模块当中,例如子程序。

下面是perl上对last用法的注意提示:

last cannot be used to exit a block that returns a value such as eval {},sub {},or do {},and should not be used to exit a grep() or map() operation.
Note that a block by itself is semantically identical to a loop that executes once. Thus last can be used to effect an early exit out of such a block.

转自:http://www.perlcn.com/perlbc/perljc/203.html

(编辑:李大同)

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

    推荐文章
      热点阅读