Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in and outputting a list of files 1

Status
Not open for further replies.

Captainrave

Technical User
Nov 16, 2007
97
GB
My code is below...it reads in two files, it compares the values in these files and outputs a new file where the new values are stored. However everytime I need it i have to change the file name each time, so basically I read in

INPUT 1 - EN638_CDS_LOCATION.xls (stays the same each time)

Input 2 - EN638_REPEAT_LOCATION5x.csv
EN638_REPEAT_LOCATION6x.csv
EN638_REPEAT_LOCATION7x.csv
EN638_REPEAT_LOCATION8x.csv all the way to 20x

Each corresponding output also has to be changed in the same way, 5x to 20x. Is there a way to automate this process rather than me changing the names EVERYtime?

Code:
use strict;
use warnings;

open(OUTPUT,"+>EN638_REPEAT_DISTRIBUTION20x.csv");

open BIG, 'D:\Genome Sequence Output\EN638\EN638_CDS_LOCATION.xls' or die "$!";
my @big = map {[split/\s+/]} <BIG>;
close BIG;

open SMA, 'D:\Genome Sequence Output\EN638\EN638_REPEAT_LOCATION20x.csv' or die "$!";
LOOP: while (<SMA>) {
   my ($s,$e) = split(/\,/);
   foreach my $array (@big) {
      if ($s >= $array->[0] && $e <= $array->[1]) {
         calculate($s,$e,$array->[0],$array->[1],$.);
         next LOOP;
      }
   }
}
close SMA;
close OUTPUT;
exit;

sub calculate {
   my ($small_start, $small_end, $big_start, $big_end, $line_num) = @_;
   my $calculation = ( ( ($big_start/$big_end) / ($small_start/$small_end) ) * 100 );
   print OUTPUT "small_start,$small_end,$big_start,$big_end,$calculation,/n";
}
 
Have you tried forming the Input 2 filename dynamically and using a while loop etc..?

Something like
Code:
while ( $i=5; $i<20; $i++ )
{
 $input2File = 'D:\Genome Sequence Output\EN638\EN638_REPEAT_LOCATION'.$i.'x.csv' ; 
 $outputFile = 'EN638_REPEAT_DISTRIBUTION'.$i.'x.csv' ;
 open(OUTPUT,"+>$outputFile");
 open SMA, $input2File or die "$!";
 ...
 ... 
 close(OUTPUT) ;
 close(SMA) ;
}



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top