Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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 ...


Comment on Re: Using grep to pick out lines in a text file
Download Code
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-12-19 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (83 votes), past polls