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";
>
>   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.

Hi Debbie.

There's a number of things wrong with your code. I doubt if its a cut/paste
from your actual code? Both input files are being opened in directory a/
for instance, and also you're never reading from them!

This is a stab at getting it going in a similar way to how you've coded it.
I don't think there's anything that's likely to cause you problems.

HTH,

Rob


    use strict;
    use warnings;

    for my $count (1 .. 960) {

        my $open;
        my $file = "lv$count.txt";

        if ( open INA, "< a/$file" ) {
            $open = open OUT, "> $file";
            die "Can't open $file: $!" unless $open;
            print OUT $_ while <INA>;
            close INA;
        }

        if ( open INB, "< b/$file" ) {
            unless ($open) {
                open OUT, "> $file";
                die "Can't open $file: $!" unless $open;
            }
            print OUT $_ while <INB>;
            close INB;
        }

        close OUT if $open;
    }




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

Reply via email to