By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

File open in for loop

P: 55
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
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
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
Expert 2.5K+
P: 4,059
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

Post your reply

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