http://www.perlmonks.org?node_id=731445


in reply to Using grep to pick out lines in a text file

You're reading the whole file into an array, which you don't need to do, if the entries are on different lines. Just read the document line by line.

I'd also say you're muddled about arrays: for each item in the array, you grep through each item in the array!

This is how I'd do it:

use strict; use warnings; open( INPUTFILE, "<WordNet.txt" ) or die "$!"; open( OUTPUTFILE, ">>WordNetTest2.txt" ) or die "$!"; print "Extracting adjectives\n"; my $previous_line; while (<INPUTFILE>) { if ( $_ =~ m/^Adj:/ ) { print OUTPUTFILE $previous_line, $_; print "found one!\n"; } $previous_line = $_; }


Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re^2: Using grep to pick out lines in a text file
by Bunta (Initiate) on Dec 19, 2008 at 05:05 UTC
    Hi Cody Pendant

    Thanks for your help! Your coding works perfectly and I've been able to change it a bit to do some other entries in the same file as well. I really appreciate your help.

    Yes, I have only been studying about Perl for around 7 days now, and it's a bit much to take in, so the more complicated (for me!) things are still eluding me. I'll keep on chugging along though!

    Thanks again for the quick reply.

    -Brad