Debbie Cooper wrote:
Please help, I am completely new to Perl but need to write a script that
does the following:

I have two directories DIR1 and DIR2 each containing files named LV1-
LV960.txt.

I need to concatenate the files in DIR1 with the files in DIR2 and put
them in DIR3.  So DIR3 will now have files named LV1-LV960.txt.

I'm using the following script:

use warnings;

for ($count = 1; $count <= 960; $count++)
{
  open OUT, ">lv${count}.txt" or die "Cant open data.txt";

in your 'die' you should include $! to see what the problem was.



if (open INA, "<a/lv${count}.txt") { print OUT $_; } close INA; } if (open INB, "<a/lv${count}.txt") { print OUT $_; } close INB; }

}
   close OUT;
This works ok except that I get a huge string of "readline on closed
filehandle INA....." and if a file is not found it is written out anyway
with a size of 0.  I'd like to not write the file at all if it's not there.
Unfortunately the numbers in the filenames are not consecutive, so althought
they go up to 960, there really are only about 242 of them.


The above has an unmatched bracket and shouldn't even compile, let alone do what you want. Was this copied or retyped? You also don't have 'use strict' which you should *always* have. You also don't seem to handle the different directories? You can use the '-e' file test operator to see if the file(s) exist to prevent 0 byte files from being created. Have a look at what I have below it should do what you want or come very close, it is untested....

http://danconia.org

--UNTESTED--

#!/usr/bin/perl
use strict;
use warnings;

# we need three directories
my $dir1 = 'DIR1';
my $dir2 = 'DIR2';
my $dir3 = 'DIR3';

# loop through the list
my $count;
for ($count = 1; $count <= 960; $count++) {

    # check for one of our two files
    if ((-e "$dir1/lv$count.txt") || (-e "$dir2/lv$count.txt")) {
        print "$count: Found at least one file for use\n";

# open a file for writing
my $OUT;
open $OUT, ">$dir3/lv$count.txt" or die "$count: Can't open file for writing: $!";


# open our files if they exist for reading, print them to our out file
if (-r "$dir1/lv$count.txt") {
my $INA;
open $INA, "$dir1/lv$count.txt" or die "$count: Can't open file 1 for reading: $!";
print $OUT <$INA>;
close $INA;
}
if (-r "$dir2/lv$count.txt") {
my $INB;
open $INB, "$dir1/lv$count.txt" or die "$count: Can't open file 2 for reading: $!";
print $OUT <$INB>;
close $INB;
}
close $OUT;
}
}


print "Done\n";


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to