in reply to Parsing log file with blank lines for the field separator

I would code that like below without storing the paragraphs in arrays and definitive not in hashes.
use strict; use warnings; my @string = ('String A', 'String B', 'String C', 'String D', 'String +E'); my @logfile = ('A.log', 'B.log', 'C.log', 'D.log', 'E.log'); my @logfh; open (IN, '<', 'input.txt') or die "Couldn't open input file!\n"; for my $i (0..$#logfile) { open ($logfh[$i], '>', $logfile[$i]) or die "Couldn't create logfi +le!\n"; } local $/ = ''; while (my $paragraph = <IN>) { for my $i (0..$#string) { if ( index($paragraph, $string[$i]) >= 0) { print {$logfh[$i]} $paragraph; } } } close IN; foreach my $fh (@logfh) { close $fh; }
This is for fixed strings. If you need regexes then replace the @string array with an array of regexes using qr{ } and replace the use of index with an regex call, ala $paragraph =~ $regex[$i].

Replies are listed 'Best First'.
Re^2: Parsing log file with blank lines for the field separator
by chromatic (Archbishop) on May 05, 2008 at 20:01 UTC

    A hash might be nicer:

    use strict; use warnings; my @strings = ('String A', 'String B', 'String C', 'String D', 'String + E'); my %string_handles = map { open my $fh, '>', "$_.log" or die "Couldn't + create '$_.log': $!"; $_ => $fh } @strings; open (my $in_fh, '<', 'input.txt') or die "Couldn't open input file!\n +"; local $/ = ''; while (my $paragraph = <$in_fh>) { while (my ($string, $out_fh) = each %string_handles) { next unless index($paragraph, $string) >= 0; print {$out_fh} $paragraph; } } close $in_fh;
      chromatic,

      I was able to use the hash you gave. Works perfectly. Could you please point me to a resource where I can learn more on hashes? I am not sure just what this code does and would like to be able to maintain it going forward.

      Thanks!

      Jason

        perldoc perldata has a good section on hashes. Think of a hash like a dictionary or a phone book. You look up a word or a name, and you get back a definition or a phone number.

        With a hash, you use a string as the key (what you put in) and you get back a scalar as the value. In this case, the keys are filenames and the values are filehandles. There's a one-to-one mapping of keys to values.

Re^2: Parsing log file with blank lines for the field separator
by JasonJ (Initiate) on May 06, 2008 at 15:51 UTC
    Thanks for all of the responses.

    I am having trouble with this bit of code. Here is what I have.

    if(-e $ERRORLOG) { print LOG "$FORMATTEDTIME Able to open the CaseError.log file.\n +"; my @string = ('FOO', 'BAR'); my @logfile = ('FOOLOG', 'BARLOG'); my @logfh; open(IN, "<$ERRORLOG") or print LOG "$FORMATTEDTIME Cant open th +e Error.log file. \n"; for my $i (0..$#logfile) { open ($logfh[$i], '>', $logfile[$i]) or die "Couldn't create logfi +le!\n"; local $/ = ''; while (my $paragraph = <IN>) { for my $i (0..$#string) { if ( index($paragraph, $string[$i]) >= 0) { print {$logfh[$i]} $paragraph; } } } close IN; foreach my $fh (@logfh) { close $fh; } } }

    I am getting the following error.

    Use of uninitialized value in ref-to-glob cast at D:\pl\ErrorCheck.pl l ine 68, <IN> chunk 1. print() on unopened filehandle at D:\pl\ErrorCheck.pl line 68, <IN> chunk 1.

    This has entried for chunk 1-8.

    I also get "readline() on closed filehandle IN at D:\pl\ErrorCheck.pl line 65."

    Any thoughts? Did I totally mess this up?