I am supposed to be writing a code to rearrange some data in some files I have. The rearranging code has been written for me (by a co-researcher). My problem now is I have to finish the file so that it'll loop through all 50 of my files. I'm in a slow process of learning, but I still am VERY new and have no idea what I am doing most of the time.
Below is the code as it is right now, to be used one file at a time. $InputFile2 will be the files that are changing name (testprefix# - where the number ranges from 162-217 - with 50 total files). Any help would be greatly appreciated.
#!/usr/bin/perl use strict; use warnings; use diagnostics; my $InputFile1 = "edited_archaea_master_list.txt"; open FILE1, $InputFile1 or die "Can't read source file: $InputFile1\n" +; my $InputFile2 = "testprefix172_single_line_nostrains_aligned_gaps_tes +t.meg"; open FILE2, $InputFile2 or die "Can't read second source file: $InputF +ile2\n"; my $OutputFile3 = "rearrangement_out_test.txt"; open FILE3, ">$OutputFile3" or die "Can't open output file: $OutputFil +e3\n"; print FILE3 "Input #1: $InputFile1\n"; print FILE3 "Input #2: $InputFile2\n\n"; my @species = <FILE1>; #take file 1 and make into an array my @align = <FILE2>; #samsies print "array @align\n"; my $limit = @align; #Number of lines read in the array my $species; #used in the foreach loop through @species array ... my $i; my $j; my $FoundFlag; #variable used to store names not found and print out a +t the end my @notFound; #used to remember species that are not found in order to + print at the end ... #loop through master list ... foreach $species(@species) { chomp($species); #deletes the enter character at the end the speci +es, so rather than looking for "species\n", it can just find "species +" #print "$species\n"; $FoundFlag = 0; #Scan second file for a match ... for ($i=0; $i<$limit; $i++) #$i is the line number, set to start a +t zero, $limit is the total number of lines in file, $i++ continues t +o every consecutive line { chomp($align[$i]); if ($align[$i] =~ /$species/) #if the line in align array matc +hes the line in the species file, then print, and will print to outpu +t file { #print "Found ....\n"; print $align[$i]; $j = $i; #$j equals to the species found in the mega f +ile, do then prints the align of $j plus all the lines that follow, u +ntil the next # sign. do { print FILE3 "$align[$j]"; $j = $j + 1; } while (($j < $limit) && !($align[$j] =~ /^#/)); #ali +gn of j will continue to print until the $limit, which is the end of +the file, AND until the next pound sign. $FoundFlag = 1; #if the species is found, then foundfl +ag equals one, meaning its true. last; #quit FOR loop ... and look for the next species } } if ($FoundFlag == 0) { print "I did not find $species\n"; #print FILE3 "I did not find $species\n"; push @notFound, $species; } #print "Next one ...\n"; } #Write not found species ... foreach my $item(@notFound) { print FILE3 "I did not find $item\n"; } close FILE3; print "Done!\n"; exit;
I apologize for all of the comments, it is the only way I know what the code is doing at this point in my learning. Again, I would greatly appreciate any help.
In reply to I'm new and unwise and need help by ishazyi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |