Hello,
I'm trying to retrieve the contexts of each keyword in an array. The problem is that I only get the right output for the last keyword in the array. Example:
FILE1:
tree
apple
FILE2:
This is a tree.
The aPple is green.
It looks like a tree.
should become:
**
<KEYWORD>tree
<CONTEXT>This is a tree.
<CONTEXT>It looks like a tree.
**
<KEYWORD>apple
<CONTEXT>The aPple is green.
I have tried to get this format with the following script, but as I said, it refuses to do the job properly ;-):
open(FILE1, "FILE1.txt"||die;
@Keywords=<FILE1>; #makes list of keywords
close(<FILE1>);
open(FILE2, "FILE2.txt"||die;
@Contexts=<FILE2>; # makes list of contexts
for($i=0; $i<=scalar(@Keywords); $i){ # for each keyword:
@foo=grep(/$Keywords[$i]/i, @Contexts); # get contexts
print "**\n"; # print FS
print "<KEYWORD>$Keywords[$i]\n"; # print the keyword
for($j=0; $j<=scalar(@foo); $j++){ # print the contexts
print "<CONTEXT>$foo[$j]";
delete $foo[$j]; # remove it from list
}
}
What should I change about this script in order to make it work? Thanks in advance. J
I'm trying to retrieve the contexts of each keyword in an array. The problem is that I only get the right output for the last keyword in the array. Example:
FILE1:
tree
apple
FILE2:
This is a tree.
The aPple is green.
It looks like a tree.
should become:
**
<KEYWORD>tree
<CONTEXT>This is a tree.
<CONTEXT>It looks like a tree.
**
<KEYWORD>apple
<CONTEXT>The aPple is green.
I have tried to get this format with the following script, but as I said, it refuses to do the job properly ;-):
open(FILE1, "FILE1.txt"||die;
@Keywords=<FILE1>; #makes list of keywords
close(<FILE1>);
open(FILE2, "FILE2.txt"||die;
@Contexts=<FILE2>; # makes list of contexts
for($i=0; $i<=scalar(@Keywords); $i){ # for each keyword:
@foo=grep(/$Keywords[$i]/i, @Contexts); # get contexts
print "**\n"; # print FS
print "<KEYWORD>$Keywords[$i]\n"; # print the keyword
for($j=0; $j<=scalar(@foo); $j++){ # print the contexts
print "<CONTEXT>$foo[$j]";
delete $foo[$j]; # remove it from list
}
}
What should I change about this script in order to make it work? Thanks in advance. J