in reply to Pattern Match Issue

If I assume that your posted code ends with two closing curly brackets, I see a clear issue. The while(<$fh>){ structure splits your file based upon the record separator, which is new line by default. Since you haven't set it ($/), that means your code is first getting fed ">CIMG_12545 | Coccidioides immitis RS hypothetical protein (translation) (95 aa)" on the first loop iteration and then "MSSQPTTLQSQTGIQRHGEISSQAQKPTTALEEDDEFEDFPVEDWPQEDAEALGPAGTNN" on the second. Therefore, you can't match across the new lines. If you want to look for patterns across new lines, you either need to slurp your file ($/) or modify your while loop to read in more than one line in an iteration (plus many other possible variations on the theme). So something like this might work:

use strict; use warnings; local $/; my ($name, $aa); while (my $seq = <DATA>) { if ($seq =~ />(\w+).+aa\)\n(\w+)/m) { $name = $1; $aa = $'; } } print "$name $aa\n"; __DATA__ >CIMG_12545 | Coccidioides immitis RS hypothetical protein (translatio +n) (95 aa) MSSQPTTLQSQTGIQRHGEISSQAQKPTTALEEDDEFEDFPVEDWPQEDAEALGPAGTNN DHLWEESWDDDDSNEEFSRQLKEELKKVEAMKQQ*

A side note, while (my $seq = <$IN2>) { is a bad structure to get into the habit of using, since it will exit your loop if the line evaluates to false - i.e. if you have a blank line in your file. Better would be while (defined (my $seq = <$IN2>)) {. See almut below.

Update: I should mention that for what you've written the m modifier on your regular expression has no impact. As it states in Modifiers, m modifies the matching behavior of ^ and $ so they match new lines. You don't have either metacharacter in your expression, so the modifier is unnecessary.

Replies are listed 'Best First'.
Re^2: Pattern Match Issue
by almut (Canon) on Nov 23, 2009 at 21:24 UTC
    ... Better would be while (defined (my $seq = <$IN2>)) {

    AFAIK, current versions of Perl implicitly do this anyway:

    #!/usr/bin/perl open my $IN2, "<", "foo.txt" or die; while (my $seq = <$IN2>) { print "line: '$seq'\n"; }
    $ perl -MO=Deparse ./808927.pl die unless open my $IN2, '<', 'foo.txt'; while (defined(my $seq = <$IN2>)) { print "line: '${seq}'\n"; } ./808927.pl syntax OK

    It's only when you write things like while (chomp(my $seq = <$IN2>)) that you run into problems... (because chomp returns the number of characters removed, which may be zero for the last line if it doesn't contain a "\n")