| |
Welcome to the dBforums forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact contact support.
If you prefer not to see double-underlined words and corresponding ads, place your cursor here for ContentLink opt out.
|
 |

08-27-08, 14:01
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
Perl - Pattern matching in sentences
|
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.
|
|

08-27-08, 14:38
|
|
Wage drone 24601
|
|
Join Date: Jan 2003
Location: Massachusetts
Posts: 3,362
|
|
Usually the <> operator returns a string, not an array. What are you expecting in the array @lines?
|
|

08-27-08, 17:34
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
hi,
First of all, thanks for your reply.
I wanted to read each line in the sentence and match the pattern derived. so, I stored all the lines in the file @lines and then for reading each line $sentences.
when I tried to run the code I sent, actually the input file is opened but it is not reading the if statement for matching the pattern and I got some error message like: "uninitialised regexp at pattern" (i.e.,) $ref.
Can you please explain me the actual problem in my code?
Thanks.
|
|

08-27-08, 17:44
|
|
Wage drone 24601
|
|
Join Date: Jan 2003
Location: Massachusetts
Posts: 3,362
|
|
The problem is that the <> operator reads one line. Check the contents of @lines after the line I am not sure what you would find in there. Could be one element, could be several elements. It is hard to tell.
|
|

08-27-08, 18:00
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
I tried to print on the screen the elements of @lines and it shows all the lines in the input file.
But how to match the pattern in each line?
|
|

08-28-08, 05:37
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
the line after @lines is the foreach loop. I could get the sentences in the input file printed on the screen. But I need each sentence and the matched pattern next to it (if loop) as the output.
advice me on how to get this.
thanks.
|
|

08-28-08, 15:50
|
|
Wage drone 24601
|
|
Join Date: Jan 2003
Location: Massachusetts
Posts: 3,362
|
|
huh. Never knew that about the <> operator.
Anyway, what is in @array_return? Or, easier to find, what is the value of $ref?
|
|

08-28-08, 19:15
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
@array_return returns the subroutine ("procedure") value where in my case it is the patterns (for eg: [A-z]oun [A-z]lock is the pattern & actual words in the file - for eg: noun/Noun Block/block) of the noun block I am trying to match in the actual text(sentences - input file).
I defined the return value as $ref (which is there in the code without any meaning at the moment) that is the pattern (right hand side) and the sentences to be matched ($sentences - on left hand side). Actually after getting @array_return, I really dont know how to get the scalar value which I now imagined as $ref.
Do you know how to derive the value of @array_return to scalar and search all sentences in the file matching that pattern?
|
|

08-29-08, 11:56
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
|
|
hi Mcrowley,
I hope you will understand my previous reply. If not, i have posted the new code for the same problem but a complete one.
Please advice me and write to me if you have any questions on understanding it.
thanks.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|