Hello perlmonks,
I am trying to match the pattern derived in all the lines that are in a input file. But, I am bit confused with my code. The code could read the sentences in the file, but not reading the next line to match the pattern in the sentences. Please help me in solving the problem. Below is my perl code:
#putting all phrases in to an array
my @phrases = ($phrase1, $phrase2, $phrase3, $phrase4);
foreach $new (@phrases) {
print STDERR "the whole phrase is: $new\n";
#defining a subroutine procedure by a variable name and calling the subroutine
@array_return = procedure($new);
print STDERR "the pattern is: @array_return\n";
for($i=0; $i < @array_return; ++$i) {
print STDERR "the value of for loop is: $array_return[$i]\n";
}
foreach $ref ($array_return[$i]) {
#open a data file containing the sentences with the phrases to be matched
$file = "sentences.txt";
open(DAT, $file) || die "Couldn't open the file: $!\n";
@lines=<DAT>;
foreach $sentences (@lines) {
print STDERR "the sentence is: $sentences\n";
#match the words in the file
if ($sentences =~ m/$ref/) {
print STDERR "the word is matched in this sentence: $sentences\n";
}elsif ($sentences =~ m/\b$ref\b/) {
print STDERR "the word is matched at the word boundary in this sentence: $sentences\n";
}
}
close DAT;
}
}
The sample sentences in the input file are at the beginning of the code. Those sentences are in the file "sentences.txt". So, I would like to match the pattern derived at the end of subroutine in the file containing some sentences.
I am sure, there is some confusion in the code either after storing the subroutine value in an array or incrementing part. I need each sentence with the pattern matched as the output. For eg:
$sentences matched ---- pattern $ref
Thanking in advance for your help and eagerly waiting.