Dodzik Dodzik - 1 month ago 10
Perl Question

Perl invalid argument when saving file

I want to compare contents of two files in my script. In order to do so I'm loading files into arrays. First file contains unique names that I'm searching in second file. Problem isn't related to comparing, but then I have to save results into file, which name contains values from

@unique_names
array. Error appears in line where I'm creating file after comparison.
Example of
$file_name
contents:
PT_FCT_Sales_20161003.out
and result of
print $outputFolder . $ file_name
is
c:\Strawberry\PT_FCT_Sales_20161003.out


foreach my $file (@Files){
if ($file =~ /out$/i){
if ($file =~ /PT_FCT_Sales_/i){
open(my $INFILE, "<", $file) or die $!;

@lines = <$INFILE>;

foreach (my $i = 0; $i < @unique_names; $i++) {
my $file_name = join('', @unique_names[$i]);
#print $file_name;
my @data;
foreach $line (@lines) {
if ($line =~ @unique_names[$i] ) {
push (@data, $line);
}
}

open(my $OUTFILE, ">>", $outputFolder . $file_name) or die $!; #error appears here
print $OUTFILE @data;
close $OUTFILE;

}
close $INFILE or die "Can't close input file";
}
}
}


EDIT: Error says: Invalid argument at test.pl line 71, line 6.
Please note this isn't whole script, just a cut off related to the problem.

Answer

here is a slightly better solution. Untested. so will edit and fix as I go along. Please provide where $outputfolder is defined.

foreach my $file (@Files){
if ($file =~ /^PT_FCT_Sales.*\.out$/i){
        open(my $INFILE,  "<", $file) or die $!;
        @lines = <$INFILE>;
        foreach (my $i = 0; $i < @unique_names; $i++) {
            my $file_name = join('', $unique_names[$i]);
            my @data;
            while (<$INFILE>) {
                if ($line =~ $unique_names[$i] ) {
                    push (@data, $line);
                }
            }
            open(my $OUTFILE, ">> $outputFolder.$file_name") or die $!; #error appears here
            print $OUTFILE @data;
            close $OUTFILE;

            }
            close $INFILE or die "Can't close input file";
        }
    }