in reply to problem in looping
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:
You can even use autodie to skip writing those checks. You can also pass file names as command-line arguments.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";
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.
|
|---|