Perl 中的 next 运算符跳过当前循环执行并将迭代器转移到 next 指定的值。如果程序中指定了标签,则执行会跳到该标签标识的下一个迭代。
用法: next Label
示例 1:
#!/usr/bin/perl -w
# Perl Program to find the frequency
# of an element
@Array = ('G', 'E', 'E', 'K', 'S');
$c = 0;
foreach $key (@Array)
{
if($key eq 'E')
{
$c = $c + 1;
}
next;
}
print "Frequency of E in the Array: $c";
输出:
Frequency of E in the Array: 2
示例 2:
#!/usr/bin/perl
$i = 0;
# label for outer loop
outer:
while ( $i < 3 ) {
$j = 0;
while ( $j < 3 ) {
# Printing values of i and j
print "i = $i and j = $j\n";
# Skipping the loop if i==j
if ( $j == $i ) {
$i = $i + 1;
print "As i == j, hence going back to outer loop\n\n";
# Using next to skip the iteration
next outer;
}
$j = $j + 1;
}
$i = $i + 1;
}# end of outer loop
输出:
i = 0 and j = 0 As i == j, hence going back to outer loop i = 1 and j = 0 i = 1 and j = 1 As i == j, hence going back to outer loop i = 2 and j = 0 i = 2 and j = 1 i = 2 and j = 2 As i == j, hence going back to outer loop
相关用法
- Perl next用法及代码示例
- Perl ne用法及代码示例
- Perl abs用法及代码示例
- Perl alarm用法及代码示例
- Perl bless用法及代码示例
- Perl caller用法及代码示例
- Perl chmod用法及代码示例
- Perl chomp用法及代码示例
- Perl chop用法及代码示例
- Perl chr用法及代码示例
- Perl continue用法及代码示例
- Perl crypt用法及代码示例
- Perl dbmclose用法及代码示例
- Perl dbmopen用法及代码示例
- Perl defined用法及代码示例
- Perl dump用法及代码示例
- Perl endhostent用法及代码示例
- Perl endnetent用法及代码示例
- Perl endprotoent用法及代码示例
- Perl endpwent用法及代码示例
- Perl endservent用法及代码示例
- Perl eof用法及代码示例
- Perl exec用法及代码示例
- Perl exists用法及代码示例
- Perl fork用法及代码示例
注:本文由纯净天空筛选整理自Code_Mech大神的英文原创作品 Perl | next operator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。