in reply to more than one condition to a while loop

Try this:

while ( <INFILE> ) { chomp; if ( /^SEQRES[\s]{2,5}1\s/ ) { print "$_\n"; print OUTFILE "$_\n"; } elsif ( /^SEQRES[\s]{2,5}2\s/ ) { last; } }

The above snippet should print everything that matches the first rexexp, and continue iterating through the loop, printing lines that match the first regexp, until a line matches the second regexp, at which time the loop exits. If the second regexp never matches, the loop exits when there's no more file to read.

Note the use of last; last basically jumps to the end of the current block or loop, as though the loop's criteria suddenly became false. continue wouldn't work, because continue just skips up until the end of the current iteration and then lets the loop continue iterating. And break doesn't work because, as you find in the index of the Camel book, "Break: See last".

Though this isn't what your code was doing, it is nevertheless, what your description of the problem said you intended to do.

Hope this helps!

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: more than one condition to a while loop
by davido (Cardinal) on Aug 21, 2003 at 17:01 UTC
    As a followup to my own post, you could make the loop more efficient by only using one regexp as follows:

    while ( <INFILE> ) { chomp; if ( /^SEQRES[\s]{2,5}(1|2)\s/ ) { if ( $1 eq "1" ) { print "$_\n"; print OUTFILE "$_\n"; } else { last; } } }

    If I thought about it more I'm sure I could clean up that ugly cascading if statement too, but this works and is reasonably efficient.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Re: more than one condition to a while loop
by anchorite (Initiate) on Aug 21, 2003 at 16:58 UTC
    Thankyou for such lightning quick responses, several of those responses all seem to work and the one with the elsif is very elegant. Your Humble Servant, Anchorite