Opening Mulitple files using For loop in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Opening Mulitple files using For loop in Perl
# 1  
Old 05-06-2009
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




Code:
#!/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
Code:
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

# 2  
Old 05-06-2009
Your problem is at line 20:
Code:
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
Code:
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)
# 3  
Old 05-06-2009
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.

Code:
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.
# 4  
Old 05-06-2009
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).
# 5  
Old 05-06-2009
Hi pludi,

Ahhh, now i am starting to understand what your codes are getting at.
Thks alot for your advice! Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop Script and not opening files containing spaces

Hello, I wrote a simple script, that basically wait for a *.dat-file in a certain folder, which is always a zipped file and extracts it. It worked before and i changed nothing in the script, but since last week i have the problem, that it doesnt extract files containing a space. How do i make... (4 Replies)
Discussion started by: blend_in
4 Replies

2. Shell Programming and Scripting

substitution to mulitple files

Hello all, I am trying to make a script that will apply a substitution to any number of files given on the command line. Example would be ~/Unix/script/subst car boat myFile1.txt myFile2.txt myFile3.txt This is the code I have so far but it does not function as needed. PAT=$1 shift... (10 Replies)
Discussion started by: ramn214
10 Replies

3. UNIX for Advanced & Expert Users

FTP failed to copy mulitple files from multiple directory

I am using below scripts to copy all the files from multiple folders. By executing individually command i am able to copy all the files but using scripts only getting first file. System is ignoring the second CD and mget command. HOST=server.com USER=loginid PASSWD="abc" echo "open $HOST... (6 Replies)
Discussion started by: meetvipin
6 Replies

4. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

5. UNIX for Dummies Questions & Answers

opening mulitple different videos with mplayer and placing them in x,y coordinates?

howdy. can you place mplayer windows on the screen anywhere? i would like to open four movie files from command line and make them show up on screen like this ----- ----- | 1 | | 2 | ----- ----- | 3 | | 4 | ----- ----- hopefully my ascii representation makes some sense. and i would... (1 Reply)
Discussion started by: danpaluska
1 Replies

6. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

7. Shell Programming and Scripting

Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.) My approach is... (1 Reply)
Discussion started by: zzlegs
1 Replies

8. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

9. Shell Programming and Scripting

Opening Files and checking contents in Perl

Hi All, I need some expert help in performing the following in Perl. I have a code below but it doesn;t seem to work. Can any expert give me some advice? Below are the requirements 1) Open numerous files assigned to an array @FILES. Note that the files are always named with the term... (7 Replies)
Discussion started by: Raynon
7 Replies

10. Shell Programming and Scripting

Opening Perl

I have gone to /usr/bin/ and click on perl but notting happens.also notting happens when i click on c/c++ or any other program whats wrong ? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question