473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

File open in for loop

Hi All,

How do we open certain number of files in for loop. In my case I have couple of files with name scd_1_2.dat here, the number 1 goes till 5 and 2 goes till 14, I am trying to open recursively but getting some error.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. for (my $i=1;$i<=5;$i++)
  6. {
  7.     for (my $j=2;$j<=14;$j++)
  8.     {
  9.         open(A, "scd_$i_$j.dat") or die "Check the file";
  10.         open(B,">step$i_$j.out");
  11.         while (<A>)
  12.         {
  13.             my $line = $_; chomp $line;
  14.             my @temp = split (/\s+/,$line);
  15.             my $carbon = $temp[2];
  16.             print B "$carbon\n";
  17.         }
  18.     }
  19. }
  20.  
The error it gives is:
Global symbol "$i_" requires explicit package name at perl.pl line 12.
Global symbol "$i_" requires explicit package name at perl.pl line 13.
Execution of perl.pl aborted due to compilation errors.

I tried to concatenate the $i and $j using "." but still not able to open file.

Any help will be appreciated.
Thanks
Kumar
Mar 19 '09 #1
2 4887
nithinpes
410 Expert 256MB
First, do not tail to an older thread. You could have created a new thread for this.

The error is shown because the variable $i_ is not defined anywhere! Instead of inserting the variable inbetween string, you should use concatenation(.) operator to concatenate the number to string.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict; 
  3. use warnings; 
  4.  
  5. for (my $i=1;$i<=5;$i++) 
  6.     for (my $j=2;$j<=14;$j++) 
  7.     { 
  8.         my $infile= "scd_".$i."_".$j.".dat";
  9.         my $out= "step".$i."_".$j.".out";
  10.         open(A, $infile) or warn "Check the file $infile:$!"; 
  11.         open(B,">$out"); 
  12.         while (<A>) 
  13.         { 
  14.             my $line = $_; chomp $line; 
  15.             my @temp = split (/\s+/,$line); 
  16.             my $carbon = $temp[2]; 
  17.             print B "$carbon\n"; 
  18.         } 
  19.        close A; close B;
  20.     } 
  21.  
Also, make a note that 'warn' is used instead of 'die' because unavailability of any one file would terminate the execution of script otherwise.
Mar 20 '09 #2
KevinADC
4,059 Expert 2GB
You can use curly brackets {} to interpolate scalars inside of double-quoted strings that have other characters in the string that cause perl to see the wrong scalar. Here it is written using perl loops and some other changes:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. for my $i (1 .. 5){
  6.    for my $j (2 .. 14){
  7.       open(my $IN, "scd_${i}_$j.dat") or warn "Can't open file [scd_${i}_$j.dat]: $!\n";
  8.       open(my $OUT, ">", "step${i}_$j.out") or warn "Can't open file [step${i}_$j.out]: $!\n";
  9.       while (<$IN>){
  10.          print $OUT (split(/\s+/))[2],"\n";
  11.       }
  12.    }
  13. }
  14.  
And as nithinpes mentioned, next time start a new thread when you have a new question.
Mar 20 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
3
by: Paulers | last post by:
Hello all, I have a log file that is appended to in real time by an application. I would like to write a loop in vb.net that reads new entires and parses them. I can get my application to open a...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
10
by: AdemusPrime | last post by:
I need to write data to a file in a loop. I get a "file already in use" error on the second loop. The problem is, I don't know the name of the file until I'm in the loop. How can I keep a file open...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.