Hello deepakshyl,
I think the other monks have all ready given you the solution and I would say that also really impressive the solution in one line from Anonymous Monk.
The reason that I am also writing in this question is to add a small part of explanation on what Laurent_R provided with really good observations.
Based on my perspective you are not an experienced coder (neither I am) so I think a few things are worth mentioning here and possibly learning.
1) chomp is most commonly used to remove the newline character "\n" from the end of an input record. It is important to remove these characters while you are reading through a file so you can extract all the strings (data in general) without any faults or extra characters.
2) Copy from you code:
use feature qw/say/;
use Math::Trig;
open FILE,"out02.txt";
my @file= <FILE>;
close FILE;
my $aa = 0;
for (my $i =$aa; $i <=17822; $i++) {
if(($file[$i] >= 0.210)){
open (OUTFILE,'>>out_t10-t10.txt');
print OUTFILE $file[$i];
}
else{}
}
You are opening the OUTFILE every time the loop occurs! In result you open the file 17822 times!
As Laurent_R proposed open your file before the loop.
3) Closing the files!!!! As far as I see you open your READ file and you close it after nice, but your WRITE file you open it 17822 times and you did not close it even once!
It is very important to close the file after use. You will flush the data into the file and you will prevent data losses.
I think the best way to open or close a file is to use a die command so you can verify that you opened the file or closed correctly!
Sample of code taken from the close FILEHANDLE (perldoc.perl.org)
open(OUTPUT, '|sort >foo') # pipe to sort
or die "Can't start sort: $!";
#... # print stuff to output
close OUTPUT # wait for sort to finish
or warn $! ? "Error closing sort pipe: $!"
: "Exit status $? from sort";
open(INPUT, 'foo') # get sort's results
or die "Can't open 'foo' for input: $!";
Hope this information will help and not confuse you more.
PS: Follow the links that I have provided you with, it will help you to get more information on what I was referring here.
Seeking for Perl wisdom...on the process...not there...yet!
|