Hi Perl Monks, the code below generates the following warning and error: 1) Name main::count used only once; and 2) Can’t use an undefined value as a symbol reference (references the line for output, which is the print $FH_OUT statement. A Google search turns up some internal Perl issues, but the issues seem to be internet related (which is not part of this code). The goal of the program is to read the first 30 or so lines of a file, saving certain information when a match occurs, then after reading the last line of the file, obtain the file size, then output the file's data. I am grateful for any thoughts anyone may have. Thank you!
#!/usr/bin/perl -w #use strict; use warnings; use File::stat; use lib "c:/strawberry/perl/site/lib"; #This program will extract the header information in 10K and 10Q filin +gs #as well as file sizes. #Specify the directory containing the files that you want to read; my $files_dir = 'C:\Test'; #Specify the directory containing the results/output; my $write_dir = 'C:\Test Output'; #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 $cik=-99; my $form_type=""; my $report_date=-99; my $file_date=-99; my $name=""; my $sic=-99; my $sic1=-99; my $HTML=0; #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; #Open the ouput file; open my $FH_OUT, '>',$write_dir.'/'.$filename or die "Can't open file +$filename"; #The following steps obtain basic data from various lines in the file; if($line=~m/^\s*CENTRAL\s*INDEX\s*KEY:\s*(\d*)/m){$cik=$1;} if($line=~m/^\s*FORM\s*TYPE:\s*(.*$)/m){$form_type=$1;} if($line=~m/^\s*CONFORMED\s*PERIOD\s*OF\s*REPORT:\s*(\d*)/m){$report +_date=$1;} if($line=~m/^\s*FILED\s*AS\s*OF\s*DATE:\s*(\d*)/m){$file_date=$1;} if($line=~m/^\s*COMPANY\s*CONFORMED\s*NAME:\s*(.*$)/m){$name=$1;} if($line=~m/^\s*STANDARD\s*INDUSTRIAL\s*CLASSIFICATION:\s*(\d*)/m){$ +sic=$1;} if($line=~m/^\s*STANDARD\s*INDUSTRIAL\s*CLASSIFICATION:.*?\[(\d{4})/ +m){$sic1=$1;} #Update line counter; ++$line_count; } #Now that we have reached EOF< grab file size, and save the results; my $filesize = -s $files_dir . '/' . $filename; my $sb = stat($files_dir . '/' . $filename)->size; print $FH_OUT "$cik,$form_type,$report_date,$file_date,$name,$sic,$sic +1,$filesize,$sb\n"; #close $FH_IN or die "unable to close $filename"; #Update file_counter; #Update file counter; ++$file_count; #Save/write results/output; print "$count lines read from $filename\n"; closedir($dir_handle); close($FH_OUT); }
In reply to Can't use undefined variable as a symbol reference by wrkrbeee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |