in reply to I'm new and unwise and need help
If you're new to Perl, some good things to read would be perlintro and Getting Started with Perl.
There are several cleanups that could be done on the script, for example using lexical filehandles (that means open my $file1, ... instead of open FILE1, ...), or waiting to define several of the variables until they're actually needed (for example for (my $i=0;...)). But regarding your question:
The two ways to do this with the smallest amount of change to your existing code would be, instead of my $InputFile1 = ..., to 1. provide the three file names via the command line and then write a quick shell script to call the script many times, or 2. if you wanted to do it all in Perl, wrap the entire code in a subroutine and call it in a loop.
1. First approach, see @ARGV on accessing the command line parameters:
die "Usage: $0 InputFile1 InputFile2 OutputFile\n" unless @ARGV==3; my ($InputFile1, $InputFile2, $OutputFile3) = @ARGV; # ... rest of the code here
Then, your script could be called from the command line or a shell script via perl script.pl edited_archaea_master_list.txt testprefix172_single_line_nostrains_aligned_gaps_test.meg rearrangement_out_test.txt
2. Second approach: Wrap all the code you've got now (except the exit, of course) into a sub process_files { ... } (see perlsub for all the details on subroutines), and get the input/output files as arguments:
sub process_files { my ($InputFile1, $InputFile2, $OutputFile3) = @_; # ... rest of your code here }
And then the sub can be called like so:
my @filesets = ( { input1 => "edited_archaea_master_list.txt", input2 => "testprefix172_single_line_nostrains_aligned_gaps_test.meg", output => "rearrangement_out_test.txt" }, # ... more file sets here ); for my $fileset (@filesets) { process_files( $fileset->{input1}, $fileset->{input2}, $fileset->{output} ); }
The data structure used for @filesets is called an "array of hashes", it is explained (including examples of how to generate them) in perldsc, and also perlreftut. You can also build @filesets dynamically by listing the files in a folder, for example using Path::Class. Also, if you use this approach, it'd be good to change the filehandles to lexical ones, as noted at the beginning.
You hint in your description that one of the input files will stay the same. In that case, the script could be optimized to read that file just once and use that data many times, instead of re-reading the same file every time. As I said, the above approaches are geared towards the least amount of change to your existing code.
|
|---|