BlueStarry
BlueStarry

Reputation: 717

How do I skip an iteration step in a while loop in Perl?

In Perl I'm trying to achieve this:

while ($row = <$fh>){
     if the row contains the character >:
          #do something AND then skip to the next line
     else:
         #continue to parse normally and do other things

Upvotes: 4

Views: 8564

Answers (2)

serenesat
serenesat

Reputation: 4709

Try this way:

while ($row = <$fh>)
{
    if($row =~ />/)
    {
        #do something AND then skip to the next line
        next;
    }
    #continue to parse normally and do other things
}

Upvotes: 3

simbabque
simbabque

Reputation: 54323

You can skip to the next iteration of a loop with the next built-in. Since you are reading line by line, that's all you need to do.

For checking if the character is present, use a regular expression. That's done with the m// operator and =~ in Perl.

while ($row = <$fh>) {
  if ( $row =~ m/>/ ) {
    # do stuff ...
    next;
  }
  # no need for else
  # continue and do other stuff ...
}

Upvotes: 11

Related Questions