仅写入文件的最后一行错误

时间:2015-10-26 21:22:31

标签: arrays printing tabs newline

问题 我在一个Windows目录中读取了一组带编号的制表符分隔的.tmp文件(即NSW1.tmp,NSW2.tmp等)&将文件名加载到数组中。

注意:每个.tmp文件在每个文件的末尾都有一个空行。

然后我逐行打开/读取每个文件,在3个元素上执行某些操作。然后将原始行+3个新变量($ capitalized,$ result& $ racenumber)重写到.txt文件中每行的末尾。除了每个文件的最后一行(即最后一行中的最后3个变量写入下一行,如下所示)之外,所有行都被正确写入。

  

Geelong Race 1 18 34374 THE ULTIMATUM 8 cdw MICHAEL HIBBS SALLY   WYNNE 58 76 87 11最后通18 18

     

Geelong Race 1 19 221x0 BROWN BURBERRY 7 dw JOHN MOLONEY BRENTON   AVDULLA a 58 57 67 10 Brown Burberry 19

     

Geelong Race 1 20 07061 STREET CRIER 11 TcDW PETER JAQUES JAMIE   MOTT 58 63 90

     

27 Street Crier 20

在上面的例子中,最后一行中的最后3个变量“27 Street Crier 20”被写入下一行。只有在为每个文件写入最后一行时才会发生这种情况。所有先前写的行都写得正确。

注意:.tmp文件在每个文件的末尾都有换行符(空白行)。如果我手动删除每个文件末尾的空行,则脚本可以正常工作。

注意2:PRINT语句很粗糙,我知道,所以我会就如何改进它提出任何建议。

THE SCRIPT

use Cwd;

$dir = getcwd;

$tab = "\t";

$final = "-FINAL";

opendir(DIR, $dir) or die $!;

while ($file = readdir(DIR)) {

   # Only read in files here (-f = file is a 'plain file').

   next unless (-f "$dir/$file");

   # Use a regular expression to find only files ending in .tmp that do not have a '-' in the filename.Note the use of the + ([a-zA-Z0-9]+) after the character class. Indicates inputs with a length greater than or equal to 1.  
   next unless ($file =~ m/^[a-zA-Z0-9]+\.tmp$/); 

   # Set variable for the state where the files are from (i.e. NSW, VIC, etc.). 
   $filecheck = substr($file, 0, 3); 

   # Push files into an array for each state.   
   push @array,$file; 

   foreach (@array) {

      open INFILE, $file or die "$nofile\n";

      # Remove .tmp extension from files & save filename for creation of new .txt files.

      $filein = $file;

      $filein =~ s/.tmp//;

      $fileout = "$filein$final.txt";   

      # Extract the race number from each filename with /(\d+)/ read for each race (i.e. NSW1.tmp). To be added to the 2nd column of output file.

      ($racenumber) = $filein =~ /(\d+)/;

      open (OUTFILE, ">$fileout"); 

      # Go through the file line by line.  

      while (my $line = <INFILE>) 

      {

         # Split the current line on tabs.

         my @words = split(/\t/, $line);

         # Set the horse name from uppercase to proper (i.e. capitalise 1st letter of each name).

         my $capitalized = join '', map { ucfirst lc } split /(\s+)/, $words[4];

         # Subtract the predicted rating from the current rating.

         my $result = $words[11]-$words[10];

         my $racenumber = $words[2];  

print "$words[0]$tab$words[1]$tab$words[2]$tab$words[3]$tab$words[4]$tab$words[5]$tab$words[6]$tab$words[7]$tab$words[8]$tab$words[9]$tab$words[10]$tab$words[11]$tab$result$tab$capitalized$tab$racenumber\n"; 

print OUTFILE "$words[0]$tab$words[1]$tab$words[2]$tab$words[3]$tab$words[4]$tab$words[5]$tab$words[6]$tab$words[7]$tab$words[8]$tab$words[9]$tab$words[10]$tab$words[11]$tab$result$tab$capitalized$tab$racenumber\n"; 

      }

   }

}


close INFILE;

close OUTFILE;

closedir(DIR);

exit;

0 个答案:

没有答案