10/25/2013

To walk through the lines of two files at once

To walk through two files at once...
The following sample Perl code read the corresponding lines of two files, and print the line which contains the joined string to another file.
  
if ($#ARGV != 1) {
    print STDERR "   Usage: perl $0 List_1 List_2\n";
    exit(1);
}

open(FileIn1, "$ARGV[0]") || die "Could not find file list $ARGV[0]";
open(FileIn2, "$ARGV[1]") || die "Could not find file list $ARGV[2]";
$outFile = "ChangeName_gbk" . ".bat";
open(FileOut, ">$outFile");

while (!eof(FileIn1) and !eof(FileIn2)) {
   my $line1 = ;
   my $line2 = ;
   chomp $line1;
   chomp $line2;
   print FileOut "rename \"$line1\" \"$line2\"\n";
}
close(FileOut);
close(FileIn1);
close(FileIn2);