Hi everyone,
I am very, very new to Perl and programming, but have been asked by my company to learn the basics (by myself, with no texts!) to help in my job.
I am trying to pluck out all the lines containing a certain word from a large text file and copy them to a new text file.
I have succeeded in doing so, but I have realized that I need to also pluck out the line PREVIOUS to the each of the ones I have already been able to grab.
Here is the code I used to grab all the lines containing "adj : "
open(FILE, "<WordNet.txt");
my @array = <FILE>;
close(FILE);
open(FILE, ">>WordNetTest2.txt");
my @array2 = <FILE>;
print "Extracting adjectives\n";
foreach $_ (@array) {
@array2 = grep {$_ =~ "adj : "} (@array);
print FILE "@array2";
close(FILE);
exit;
}
Here is a sample of what the lines look like from the text file:
-----------------
Fast
adj : quick, speedy
-----------------
I've grabbed all the "adj : " lines, but the others have eluded me!
Would there happen to be anyone who could instruct me on how to take out the previous lines along with all lines containing "adj : "?
Thank you very much in advance for any help.
-Brad
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|