Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Using grep to pick out lines in a text file

by Cody Pendant (Prior)
on Dec 19, 2008 at 03:21 UTC ( #731445=note: print w/replies, xml ) Need Help??

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://731445]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-02-28 12:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found