PERL Array fails to read in complete text file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kalpesh Katwala
    New Member
    • Aug 2010
    • 3

    PERL Array fails to read in complete text file.

    I'm trying to read in a data file into an array. And split it into lines. I've a for loop which will parse the array, split the line (on ",") and execute another command. Here is the code.

    Problem is that PERL will parse only the last 11 lines from the data file.

    Code:
    open TestList, "Testlist.dat" or die "Cannot open file to read data\n";
    @array = <TestList>;
    close TestList;
    print (@array);   <<< This prints the complete data file - all 22 line >>>
    
            $test = pop (@array);
    foreach $array (@array ) {
            @tests = split(/,/,$test);
            print @test;    <<< This prints the last 11 lines only >>
            $telnet->print("cd /root/kal/ameritec/");
            $telnet->print("mkdir $tests[0]");
            $telnet->print("cd $tests[0]");
            $TNdir = $tests[0];
            $TN1 = $tests[1];
            $TN2 = $tests[2];
            print ("$TNdir is being run - $TN1, $TN2");
    #       $telnet->print("nohup ../analyzeTrace.sh /root/kal/AM/$TNdir $TN1
    $TN2 &");
    #       sleep (300);
            $test = pop (@array);
            $n++;
            }
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Why are you needlessly slurping the file into an array?

    Code:
            print @test;    <<< This prints the last 11 lines only >>
    Based on the code you posted, that won't print anything.

    Why are you using:
    Code:
            $test = pop (@array);
    The net effect of that will be that you're processing every other line.

    Obviously you're not running under strictures. Please add these 2 lines near the beginning of the script and remove those pop lines.
    Code:
    use strict;
    use warnings;

    Comment

    • Kalpesh Katwala
      New Member
      • Aug 2010
      • 3

      #3
      Thank you Ron. I took out the pop statements and I modified the code as follows and it works now.

      Code:
      foreach (@array) {
              my @tests = split(/,/,$_);
              print $tests;
              $telnet->print("cd /root/kal/ameritec/");
              $telnet->print("mkdir $tests[0]");
              $telnet->print("cd $tests[0]");
              my $TNdir = $tests[0];
              my $TN1 = $tests[1];
              my $TN2 = $tests[2];
              print ("$TNdir is being run - $TN1, $TN2");
              $telnet->print("nohup ../analyzeTrace.sh /root/kal/ameritec/$TNdir $TN1 $TN2
      &");
      #       sleep (300);
      }

      Comment

      Working...