Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 400
BlueStarry Avatar asked Oct 25 '25 02:10

BlueStarry


1 Answers

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 ...
}
like image 119
simbabque Avatar answered Oct 26 '25 17:10

simbabque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!