To my mind, you have a logic issue long before you have a regex syntax issue.
Does the first phrase ($var[0]) always define the full version of the phrase? If so, ok, straight forward enough. But if not, then how do you decide which phrases define what is "normal" to be in the phrase, and which have a variant? The human brain may be able to see that naturally enough, but your code has no element of that.
If the first phrase always defines the complete phrase, then, easy:
#!/usr/bin/perl -w
use strict;
use warnings;
print "Content-type:text/html\n\n";
my @var;
$var[0] = "Gallia est omnis divisa in partes tres";
$var[1] = "Gallia est omnis divisa in ...";
$var[2] = "Gallia est omnis ...";
$var[3] = "Gallia";
$var[4] = "... omnis divisa in ...";
$var[5] = "Gallia est ... tres";
$var[6] = "Gallia ... partes tres";
$var[7] = "Gallia est ... partes tres";
$var[8] = "Gallia ... divisa ... tres";
$var[9] = "... tres";
$var[10] = "quattuor";
my @base_phrase_words = split(/\s/, $var[0]);
push(@base_phrase_words, "...");
my %words = map { $_ => 1 } @base_phrase_words;
for (my $i=1; $i<=$#var; $i++) {
my @partial_phrase_words = split(/\s/, $var[$i]);
foreach my $element (@partial_phrase_words){
if (exists($words{$element})) {
# do whatever
}
else {
print "<p>Found word $element in phrase $i varies from bas
+e phrase\n";
}
}
}
Output
Found word quattuor in phrase 10 varies from base phrase
Time flies like an arrow. Fruit flies like a banana.
-
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.
|