First of all, use warnings and strict if your script is longer than several lines. It is really a good practice which will help you to avoid many errors.

Secondly, it is recommended to use more modern and more safe three-argument form of open, using scalar variables as filehandles:

open my $file1,"<","1.txt" or die "1.txt: $!\n"; open my $file2,"<","2.txt" or die "2.txt: $!\n"; open my $w1,">","3.txt" or die "3.txt: $!\n"; open my $w2,">","4.txt" or die "4.txt: $!\n";
You can even use autodie to skip writing those checks. You can also pass file names as command-line arguments.

Thirdly, you can process both files in one while loop, if you want to just check the data of the same line numbers:

while ( defined(my $line1=<$file1>) and defined(my $line2=<$file2>) ) { if (abs( (split"\t",$line1)[2]-(split"\t",$line2)[2] )>=2) { print $w1 "$line1\n"; print $w2 "$line2\n"; } }

Fourthly, remember: indenting is your friend. It helps to understand the structure if your program. Run your code through Perl::Tidy, set up your editor to indent the text for you, or install the Perl IDE.

Note: the code samples are untested and may fail for some reason.

Sorry if my advice was wrong.

In reply to Re: problem in looping by aitap
in thread problem in looping by Anonymous Monk

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.