Can I pull the next element from within a Perl foreach loop?

Posted by Thilo on Stack Overflow See other posts from Stack Overflow or by Thilo
Published on 2010-06-05T08:52:06Z Indexed on 2010/06/05 9:02 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

Can I do something like the following in 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;
    }

}

© Stack Overflow or respective owner

Related posts about perl

Related posts about loop