Opening Mulitple files using For loop in Perl

Hi All,

I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file.
Unfortunately, i encounter the below syntax error.
I think there should be something wrong with this term reports_${counting}_${_[0]}.txt but i do not know how to modify the code to suit this.
Can any expert help me with this ?

The file names which i wish to open are :
reports_1_MAIN
reports_2_MAIN
reports_3_MAIN
...
...
reports_10_MAIN

#!/usr/local/bin/perl

use Env;               # for processing environment variables
use strict;
use warnings;

my $test = "MAIN";
capture_LOT_DATE ($test);
 

sub capture_LOT_DATE {
            my $counting = 1;
            my @fields;
            my @DATE;
            my @date;
            my @lotsuffix;
   
             for ($counting = 1; $counting <= 10 ; $counting++ ) {
                     open(FH, "< $HISTORY/reports_${counting}_${_[0]}.txt" ) or die $!;
                             while( <FH> ) {
                                     my @Fld = split(' ', reports_${counting}_${_[0]}.txt);
                                     if ( $Fld[1] eq 'LOTSUFFIX=' ) {
                                             $lotsuffix[$counting] = $Fld[2];
                                             last;
                                     }
                                    if ( $Fld[1] eq 'DATE=' ) {
                                             $DATE[$counting] = $Fld[2];
                                            @fields = split(/-/, $DATE[$counting]);
                                             $date[$counting] = sprintf {"%s-%s",$fields[1],$fields[2]};
                                     }
                             }
                             close FH;
             }
}

Syntax Error

Bareword found where operator expected at near "${counting}_"
        (Missing operator before _?)
Bareword "txt" not allowed while "strict subs" in use 
Bareword "files_extracted" not allowed while "strict subs" in use 
Bareword "txt" not allowed while "strict subs" in use 

Your problem is at line 20:

my @Fld = split(' ', reports_${counting}_${_[0]}.txt);

You're trying to do a split on the filename itself, and it's written as a Perl statement/variable. Use $_ instead, or change your while statement to

while($line = <FH>)

and split $line.

May I also suggest 2 (for me invaluable) tools: perl -c will check the syntax of your code without running it, and perltidy will format your code for better readability (does a bit of syntax checking, too)

Hi pludi,

I am trying to spilt out all the strings in the file name reports_${counting}_${_[0]}.txt, so that i can extract out the 2nd field if the 1st field match.

my @Fld = split(' ', reports_${counting}_${_[0]}.txt);

Perhaps i have some coding logic error here which may have mis-led you.
What i am trying to do exactly here is to

1) Open each of the 10 files with file name "reports_1_MAIN" etc
2) After opeing each file, search the content in each of the file.
if 1st field in the file equals 'LOTSUFFIX=' , print the 2nd field
and
if 1st field in the file equals 'DATE=' , print the 2nd field

Maybe you can give me some guidelines on how to modify the below code such that i can get it to perform the above.

Yes, that's what I meant. The outer for loop will open each of the files (in sequence). The while loop will read each line of the file (in sequence) and split it, if you supply the correct variable ($_ or $line in my post).

Hi pludi,

Ahhh, now i am starting to understand what your codes are getting at.
Thks alot for your advice! :b: :slight_smile: