To answer a few more questions:
while ((my $line = <INFILE>) && ($line !~ /SEQ.../)) (Removed as per tye's second comment. Just look below - it's better code anyways.)
- It's Perl, not PERL
- The regex match operator is =~. The regex not-match operator is !~. It's an atomic 2-character-long operator, not something like += or -=.
- 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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.