in reply to more than one condition to a while loop

To answer a few more questions:
  1. while ((my $line = <INFILE>) && ($line !~ /SEQ.../)) (Removed as per tye's second comment. Just look below - it's better code anyways.)
  2. It's Perl, not PERL
  3. The regex match operator is =~. The regex not-match operator is !~. It's an atomic 2-character-long operator, not something like += or -=.
  4. Rewrite as such:
    while (my $line = <INFILE>) { # Do a match against the line, grabbing the number. my ($num) = $line =~ /SEQRES\s{2,5}(\d)\s/; # This means that the line didn't meet our requirements # as defined by the regex. So, go get the next line. # (I added this ... you may not want it, depending on # what else you're doing in the loop.) next unless defined $num; # If that number is 2, leave the while loop. last if $num == 2; # Don't chomp unless there's a possibility we'll use it. chomp $line; # If the number is 1, print it to two places. if ($num == 1) { print "$line\n"; print OUTFILE "$line\n"; } }
Make it obvious what the difference is between the two matches. I assigned the difference to $num, which was done via the capture mechanism of regexes. (Look for the parentheses.)

Update: Changed code per tye's comments.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re^2: more than one condition to a while loop (bug?)
by tye (Sage) on Aug 21, 2003 at 16:57 UTC
    while (my $line = <INFILE> && $line !~ /SEQ.../)

    I'm usually suspicious of "my" inside conditionals. In this case, your code won't even compile under strict as you are trying to assign the value of

    <INFILE> && $line !~ /SEQ.../
    to my $line.

    Update: Your new version doesn't fix the problem:

    while ((my $line = <INFILE>) && ($line !~ /SEQ.../))
    still produces:
    Global symbol "$line" requires explicit package name
    because my doesn't declare a variable until the end of the statement that it occurs in (as I understand it).

                    - tye
Re: Re: more than one condition to a while loop
by sauoq (Abbot) on Aug 21, 2003 at 21:21 UTC
    2. It's Perl, not PERL

    In the sense that he is using it, it's probably perl or perl.exe. If it is the latter, it probably works when he calls it PERL. ;-)

    -sauoq
    "My two cents aren't worth a dime.";