wrkrbeee has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I'm reading an HTM file line by line, then saving the results ($line) in a text file (to be manipulated further). However, the code below fails to generate any output. What am I missing? My apologies. Thank you for any suggestions!
#!/usr/bin/perl -w use strict; use warnings; use File::stat; use lib "c:/strawberry/perl/site/lib"; #This program will extract Exhibit 21 info from 10-K filings; #Specify the directory containing the files that you want to read; my $files_dir = 'E:\research\audit fee models\filings\Test'; #Specify the directory containing the results/output; my $write_dir = 'E:\research\audit fee models\filings\filenames\filen +ames.txt'; #Open the directory containing the files you plan to read; opendir(my $dir_handle, $files_dir) or die "Can't open directory $!"; #Initialize file counter variable; my $file_count = 0; #Loop for reading each file in the input directory; while (my $filename = readdir($dir_handle)) { next unless -f $files_dir.'/'.$filename; print "Processing $filename\n"; #Initialize the variable names. my $line_count=0; my $access_num=""; my $cik=-99; my $name=""; my $stuff=""; my $line=""; #Open the input file; open my $FH_IN, '<',$files_dir.'/'.$filename or die "Can't open $filen +ame"; #Within the file loop, read each line of the current file; while (my $line = <$FH_IN>) { next unless -f $files_dir.'/'.$filename ; if ($line_count > 500000) { last;} ++$line_count; } #Open the ouput file; open my $FH_OUT, '>>',$write_dir or die "Can't open file $write_dir"; #Save the output to disk; print $FH_OUT "$line\n"; #close $FH_IN or die "unable to close $filename"; #Update file counter; ++$file_count; print "$line_count lines read from $filename\n"; #closedir($dir_handle); close($FH_OUT); }

Replies are listed 'Best First'.
Re: Read HTM, but no output
by Corion (Patriarch) on Apr 08, 2016 at 21:35 UTC

    Look at where you read the input from $FH_IN. In that loop, you never print the lines you read.

    If you want to read from a file and write to a second file, you need to do that within one loop:

    open my $FH_IN, ... or die; open my $FH_OUT, ... or die; while( my $line = <$FH_IN>) { ... print $FH_OUT $line; }

    If you apply proper indentation to your file, you will immediately spot where each loop of your program starts and ends. You have one outer loop which iterates over the entries in a directory, but in your inner loop you have again a check for the existence of the file. Clean up the sprinkled logic and you're all set.

      Very helpful! Thank you!!
Re: Read HTM, but no output
by 1nickt (Canon) on Apr 08, 2016 at 21:32 UTC

    Hi wrkrbeee,

    You need to put your print statement inside the loop where you are reading the lines.

    You should also open your output file outside both the loops.

    Is this your actual code? I would have thought trying to use $line outside the scope where you declare it (the inner loop) would cause an error under strict.
    You also shouldn't declare my $line outside the loop it's used in, and definitely not twice.


    The way forward always starts with a minimal test.
      That would do it ... stupid me! Thank you!
      Thanks again!