File open in for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    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.
    [CODE=perl]
    #!/usr/bin/perl
    use strict;
    use warnings;

    for (my $i=1;$i<=5;$i++ )
    {
    for (my $j=2;$j<=14;$j+ +)
    {
    open(A, "scd_$i_$j.dat" ) or die "Check the file";
    open(B,">step$i _$j.out");
    while (<A>)
    {
    my $line = $_; chomp $line;
    my @temp = split (/\s+/,$line);
    my $carbon = $temp[2];
    print B "$carbon\n" ;
    }
    }
    }
    [/CODE]

    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
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    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.
    Code:
    #!/usr/bin/perl 
    use strict; 
    use warnings; 
      
    for (my $i=1;$i<=5;$i++) 
    { 
        for (my $j=2;$j<=14;$j++) 
        { 
            my $infile= "scd_".$i."_".$j.".dat";
            my $out= "step".$i."_".$j.".out";
            open(A, $infile) or warn "Check the file $infile:$!"; 
            open(B,">$out"); 
            while (<A>) 
            { 
                my $line = $_; chomp $line; 
                my @temp = split (/\s+/,$line); 
                my $carbon = $temp[2]; 
                print B "$carbon\n"; 
            } 
           close A; close B;
        } 
    }
    Also, make a note that 'warn' is used instead of 'die' because unavailability of any one file would terminate the execution of script otherwise.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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:

      Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
       
      for my $i (1 .. 5){
         for my $j (2 .. 14){
            open(my $IN, "scd_${i}_$j.dat") or warn "Can't open file [scd_${i}_$j.dat]: $!\n";
            open(my $OUT, ">", "step${i}_$j.out") or warn "Can't open file [step${i}_$j.out]: $!\n";
            while (<$IN>){
               print $OUT (split(/\s+/))[2],"\n";
            }
         }
      }
      And as nithinpes mentioned, next time start a new thread when you have a new question.

      Comment

      Working...