Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple files processing - some empty

Status
Not open for further replies.

williey

Technical User
Jan 21, 2004
242
0
0
Hi,
The script below read a list of text files that meets the wildcard criteria from a directory. It loops through all the files and rewrite to a new file after discarding some records.

For some reason(s), some files are empty. If I were to limit the number of files to process, the output files are successfully written. Is the problem due to filehandle conflicts?

Code:
foreach $file(@files)
  {
    $outfile=substr($file, 0, length($file) - 4) .  ".dat";

    print("Processing " . $file . "\n");
    print("Output file: " . $outfile . "\n");

    open(INFILE, "$file") or die "File does not exist: $file !";
    open(TMPFILE, ">$outfile") or die "File does not exist: $outfile !";

    while ($line = <INFILE>) 
      {
        if ($line =~ /$pattern/i)
          {
	       if ($set2_found eq 'N')
                 {
                    $line3 = substr($line, 0, 44);
                    print(TMPFILE "$line3");
                 }

            $set2_found = 'Y';
          }

        if ($set2_found ne 'Y')
          {
             print(TMPFILE "$line");
          }
      }
    print("Processing completed\n");
#   sleep(6);
   close (INFILE);
   close (TMPFILE);


------------------------------------------
There are 10 kinds of people in this world. One that understands binary and the other one that does not.
 
where does $set2_found get set the first time? What does $pattern contain?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Follow up to travis' question - where does $set2_found get reset to N for the next file?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
It was set outside the loop. 3 out of 5 files do not have set 2, thus the first 3 files processed fine. Thanks guys for catching it..

------------------------------------------
There are 10 kinds of people in this world. One that understands binary and the other one that does not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top