Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以从Perl foreach循环中提取下一个元素吗?_Perl_Loops_Foreach - Fatal编程技术网

我可以从Perl foreach循环中提取下一个元素吗?

我可以从Perl foreach循环中提取下一个元素吗?,perl,loops,foreach,Perl,Loops,Foreach,我可以在Perl中执行以下操作吗 foreach (@tokens) { if (/foo/){ # simple case, I can act on the current token alone # do something next; } if (/bar/) { # now I need the next token, too # I want to read/consume it, adv

我可以在Perl中执行以下操作吗

foreach (@tokens) {
     if (/foo/){
       # simple case, I can act on the current token alone
       # do something
       next;
    }
    if (/bar/) {
       # now I need the next token, too
       # I want to read/consume it, advancing the iterator, so that
       # the next loop iteration will not also see it
       my $nextToken = .....
       # do something
       next;
    }

}

更新:我需要在Perl中使用它,但出于好奇:其他语言是否有简洁的语法

不使用
foreach
循环。您可以为循环使用C样式的

for (my $i = 0; $i <= $#tokens; ++$i) {
  local $_ = $tokens[$i];
  if (/foo/){
     next;
  }
  if (/bar/) {
    my $nextToken = $tokens[++$i];
    # do something
    next;
  }
}

for(my$i=0;$i您必须使用
for
循环吗?复制原件并使用以下内容“使用”它:

从Perl 5.12开始,通过处理阵列:

use 5.012;
use warnings;

my @tokens = 'a' .. 'z';

while (my ($i, $val) = each @tokens) {
    if ($val =~ m/[aeiou]/) {
        ($i, $val) = each @tokens;   # get next token after a vowel
        print $val;
    }
}

# => bfjpv

需要注意的是,请记住迭代器是全局的,并且在跳出循环时不会重置

例如:

while (my ($i, $val) = each @tokens) {
    print $val;
    last if $i == 12;
}

# => abcdefghijklm

my ($i, $val) = each @tokens;
say "Now at => $val ($i)";         # Now at => n (13)
因此,请使用或手动重置迭代器:

keys @tokens;                      # resets iterator
($i, $val) = each @tokens;
say "Now at => $val ($i)";         # Now at => a (0)

我喜欢Zaids的答案,但如果它在数组中遇到nul元素,它就会失败 所以


@array
为空之前,此操作不会停止。

很好。在我的情况下,销毁原件甚至都不是问题,所以我可以不使用副本。哇!我不知道--这会很方便。谢谢:)这是最好的解决方案。
    my $arr = [0..9];

    foreach ( 1 ..  scalar @{$arr} ) {

           my $curr = shift @{$arr};

           my $next = shift @{$arr};

           unshift @{$arr} , $next;

           print "CURRENT :: $curr :: NEXT :: $next \n";
    }
while (@array) {

    my $token = shift @array;
    shift @array if $token =~ /[nr]/; # consumes the next element
    print $token;
} 
    my $arr = [0..9];

    foreach ( 1 ..  scalar @{$arr} ) {

           my $curr = shift @{$arr};

           my $next = shift @{$arr};

           unshift @{$arr} , $next;

           print "CURRENT :: $curr :: NEXT :: $next \n";
    }