in reply to Perl File Parsing - My Code Works, but it's Ugly!

Hi Nico,

Welcome to the monastery!

Your code does not compile. Could you share your working code?

  • Comment on Re: Perl File Parsing - My Code Works, but it's Ugly!

Replies are listed 'Best First'.
Re^2: Perl File Parsing - My Code Works, but it's Ugly!
by Nico (Novice) on May 31, 2015 at 16:08 UTC

    Sorry about that, let me see if I can paste a working portion in here

    #!/usr/bin/perl use 5.010; use Time::Local; my $file_01_txt = "file_01.txt"; ($sec, $min, $hour, $mday, $mon, $year, $j1,$j2, $j3) =localtime(time) +; $year = $year+1900; $mon = $mon+1; $outfolder = "REPORT_output\_$year\_$mon\_$mday\_\_$hour\_$min\_$sec"; mkdir $outfolder; mkdir "$outfolder/log"; $html_file = <$outfolder/REPORT.html>; $log_file = <$outfolder/log/log.txt>; open($html_report, '>', $html_file) or die "Cannot open file $html_fil +e"; open($log_report, '>', $log_file) or die "Cannot open file $log_file"; main(); sub main { if (-e $file_01_txt) { # We are going to open the log file for appending. open($log_report, '>>', $log_file) or die "Cannot open $log_fi +le."; # We are going to open the HTML file for appending. open($html_report, '>>', $html_file) or die "Cannot open file +$html_file"; # If the file exists, we are going to open it, so we can read +from it. print $log_report "$file_01_txt has been found.\n"; open (my $fh, "<", $file_01_txt) or die "Cannot open file $fil +e_01_txt"; print $html_report "\n"; print $html_report "#### Now Analyzing '$file_01_txt'\n"; print $html_report "\n"; while (my $line = <$fh>) { chomp $line; if ($line =~ /search criteria 01/i) { print $html_report "$line\n"; } if ($line =~ /search criteria 02/i) { print $html_report "$line\n"; } if ($line =~ /search criteria 03/i) { print $html_report "$line\n"; print $html_report "\n"; } } close $log_report; close $html_report; } else { # We are going to open the log file for appending. open($log_report, '>>', $log_file) or die "Cannot open $log_fi +le."; # We are going to log that the file has not been found. print $log_report "$file_01_txt has not been found.\n"; close $log_report; } }

    That should work. I just ran it and everything came back as it should.

    Please let me know if I need to give you anything else!