[EMAIL PROTECTED] (Andrea Holstein) wrote:
> Prasanthi Tenneti wrote:
>>
>> Iam a beginner in perl.I have one question, Iam trying to write one
>> prog,in which i have to search for one word in a file, If I found that
>> word,print next 4 lines. PLs help me,how to write code.
>
> open FILE, "<filename";
Always test the result of open().
open FILE, "<filename" or die "Cannot open file: $!";
> while (<FILE>) {
> print(<FILE>,<FILE>,<FILE>,<FILE>), last if /your_word/;
> }
> close FILE;
Did you test that?
print <FILE>;
will print the rest of the file.
print() expects a list. <> in list context returns rest of whatever file
it's reading, one line per list element.
The same question -- with a trivial difference -- was asked a few days
ago.
use strict;
use warnings;
open FILE, "<filename" or die "Cannot open file: $!";
while (<FILE>) {
next unless /your_word/;
$_ = <FILE>, print for 1..4;
last;
}
close FILE, "<filename" or die "Cannot close file: $!";
Alternately, the loop could be rewritten to be (perhaps) a little clearer:
while (<FILE>) {
next unless /your_word/;
for (1..4) {
$_ = <FILE>;
print;
}
last;
}
--
David Wall - [EMAIL PROTECTED]
"When the end of the world comes, I want to be in Cincinnati. Everything
happens ten years later there." -- Mark Twain
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]