in reply to Newbie Question on parsing files
Perl has a paragraph input mode which is activated by setting the variable $/ to an empty string. (See perlvar for more.) That lets you process a file by paragraphs as you read it.
Here is a revised version of your code (untested):
I have kept your approach using split() to isolate the part before ":", but a regular expression simplifies things:my $KWD = shift; my $file = 'input.txt'; open my $in, '<', $file or die "Can't read '$file': $!"; local $/ = ''; while ( my $para = <$in> ) { my ( $first) = split /:/, $para; print $para if $first eq $KWD; }
Anno/^$KWD:/ and print while <in>;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Newbie Question on parsing files
by jping45 (Initiate) on Mar 27, 2007 at 18:11 UTC |