Perl next operator

by Nithya 2010-02-04 14:01:07

Solution

When you're in a Perl for loop (iterating over an array or hash), and you want to move on to the next element in your array or hash, just use the Perl next operator, like this:

Example 1:

for (@array)
{
if (SOME_TEST_CONDITION)
{
# move on to the next loop element
next;
}

# more code here ...
}

Example 2:

for (@records)
{
# skip blank lines
next if /^[ \t]*$/;

# skip comment lines
next if /#/;

# skip printing lines
next if /print/;
next if /printf/;

# much more code here ...

}

Tagged in:

652
like
0
dislike
0
mail
flag

You must LOGIN to add comments