http://qs321.pair.com?node_id=762908

patric has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have an input file with 7 columns. Now, i am concerned only about the 4th column because, based on that column, i will have to segregate each and every line to specific result files.
input file: 11880 13417 - S00010GM001 sml_056 sp|YV02233 desc 13804 14685 - S00010GM002 sml_045 sp|YV02643 desc 15525 18026 - S00001GM001 sml_032 sp|V023334 desc 32763 34239 + S00002GM001 sml_028 sp|YV02376 desc 67929 68933 - S00003GM001 sml_025 sp|YV02346 desc 90562 91368 + S00012GM001 sml_025 sp|YV02376 desc 10209 10433 - S00012GM002 sml_046 sp|YV02355 desc 12522 12576 + S00013GM001 sml_027 sp|0235777 desc 13247 13349 - S00013GM002 sml_088 sp|YV02375 desc
The 4th column is designed like this. "S","5 digit number","GM","3 digit number". the five digit number ranges from 1-35000. the three digit number ranges from 1-999. Now, my only concern is about the five digit number,because i have create an output file on the five digit number(excluding the preceeding zeros) and write all the lines corresponding to that id to the output file. before you get confused on my query, i will tell you an example. In the sample input file i have given above, the five digit number after "S" ranges from 1-13.(which is given as 00001 to 00013. thats how the format is.)So, i will have to generate 13 different output files and segregate the lines belonging to that id to their output files. example:
sample output files: filename:output_1.txt 15525 18026 - S00001GM001 sml_032 sp|V023334 desc filename:output_2.txt 32763 34239 + S00002GM001 sml_028 sp|YV02376 desc filename:output_3.txt 67929 68933 - S00003GM001 sml_025 sp|YV02346 desc filename:output_4.txt NO HITS files 5 to 9 = same as above(like output 4, because there are no S0000 +5 files found, neither the others till S00009). filename:output_10.txt 11880 13417 - S00010GM001 sml_056 sp|YV02233 desc 13804 14685 - S00010GM002 sml_045 sp|YV02643 desc files 11 and 12 = same as above (like output 10, because there are hit +s found with S00011 and S00012).
Hope the query is clear now. But my program gives me, all empty files :( it writes only one output file correctly. Only the first output file. The rest are empty.Here is my program:
#!/usr/bin/perl use strict; use warnings; open(FH,"input.txt")or die "can not open input file\n"; for(my $i=1;$i<=15;$i++){ open(OUT,">output_$i.txt") or die "can not create new files\n"; my $pattern=sprintf '%05s',$i; $pattern="S".$pattern."GM"; my $c=0;my $search; while(my $line=<FH>){ my($one,$two,$three,$four,$five,$six,$seven)=split("\t",$line) +; if($four=~m/(S\d+GM)/){ $search=$1; $search=~s/\s+//g; } if($search=~m/$pattern/){ print OUT "$line"; $c++; } } if($c==0){ print OUT "NO HITS\n"; } $pattern=();$search=(); }
where am i going wrong??? please help.