perl_n00b has asked for the wisdom of the Perl Monks concerning the following question:

You guys having been a lifesaver so far! I have one more easy question though...
Why does this pattern match not search past the new line after "(95 aa)"?
while (my $seq = <$IN2>) { if ($seq =~ />(\w+).+aa\)\n(\w+)/m) { $name = $1; $aa = $';
>CIMG_12545 | Coccidioides immitis RS hypothetical protein (translatio +n) (95 aa) MSSQPTTLQSQTGIQRHGEISSQAQKPTTALEEDDEFEDFPVEDWPQEDAEALGPAGTNN DHLWEESWDDDDSNEEFSRQLKEELKKVEAMKQQ*

Thanks in advance!

Replies are listed 'Best First'.
Re: Pattern Match Issue
by kennethk (Abbot) on Nov 23, 2009 at 21:01 UTC
    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.

      ... 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")

Re: Pattern Match Issue
by JadeNB (Chaplain) on Nov 23, 2009 at 21:02 UTC
    Your code doesn't compile as presented; I assume that you mean to have two } at the end of the while loop, and that the contents after > is what's in $IN2? In this case, the problem is that the IRS $/ is set to be a newline, so the string is read as 3 separate lines, rather than 1 big one as you'd hope. You could ‘slurp’ by using local $/, if that's the effect that you want.
Re: Pattern Match Issue
by BioLion (Curate) on Nov 24, 2009 at 12:34 UTC