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.


In reply to Re: I'm new and unwise and need help by Anonymous Monk
in thread I'm new and unwise and need help by ishazyi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.