In the original you are searching for two strings in the same line but in the new version you are searching for one string in one line and the other string in the next line.

Also, you are using the high precedence || operator which means that the program will not die if open fails because you are testing the boolean state of the string 'sample.fa' or the string '>modsample.fa' which are always true.

You probably want something like this:

open IN, '<', 'sample.fa' or die "Could not open 'sample.fa' becau +se: $!"; open OUT, '>', 'modsample.fa' or die "Could not open 'modsample.fa' be +cause: $!"; my $previous; while ( my $line = <IN> ) { if ( $previous && $line =~ /mamma/ && $line =~ /mia/ ) { print OUT $previous, $line; } else { print "."; } $previous = $line }

In reply to Re: What went wrong with this change in code? by jwkrahn
in thread What went wrong with this change in code? by naturalsciences

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.