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

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;

Replies are listed 'Best First'.
Re^3: Parsing log file with blank lines for the field separator
by JasonJ (Initiate) on May 06, 2008 at 18:19 UTC
    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.