Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: problem in looping

by aitap (Curate)
on Aug 13, 2012 at 23:35 UTC ( [id://987240]=note: print w/replies, xml ) Need Help??


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:

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://987240]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found