Нужно ли поддерживать дублирующиеся слова?
#! /usr/bin/perl
use strict;
use warnings;
use constant DEBUG => 0;
my @words;
if($ARGV[0] && -f $ARGV[0]) {
open my $fh, "<", $ARGV[0] or die "Could not read $ARGV[0], because: $!\n";
my $hughTestFile = do { local $/; <$fh> };
@words = split /[\s\n]/, $hughTestFile; # $#words == 10M words with my test.log
# Test words (below) were manually placed at equal distances (~every 900K words) in test.log
# With above, TESTS ran in avg of 15 seconds. Likely test.log was in buffers/cache.
} else {
@words = qw(word1 word2 word3 word4 word5 word6 word7 word8 word4 word9 word0);
}
sub IndexOf {
my $searchFor = shift;
return undef if(!$searchFor);
my $Nth = shift || 1;
my $length = $#words;
my $cntr = 0;
for my $word (@words) {
if($word eq $searchFor) {
$Nth--;
return $cntr if($Nth == 0);
}
$cntr++;
}
return undef;
}
sub Distance {
# args: <1st word>, <2nd word>, [occurrence_of_1st_word], [occurrence_of_2nd_word]
# for occurrence counts: 0, 1 & undef - all have the same effect (1st occurrence)
my($w1, $w2) = ($_[0], $_[1]);
my($n1, $n2) = ($_[2] || undef, $_[3] || undef);
die "Missing words\n" if(!$w1);
$w2 = $w1 if(!$w2);
my($i1, $i2) = (IndexOf($w1, $n1), IndexOf($w2, $n2));
if(defined($i1) && defined($i2)) {
my $offset = $i1-$i2;
print " Distance (offset) = $offset\n";
return undef;
} elsif(!defined($i1) && !defined($i2)) {
print " Neither words were ";
} elsif(!defined($i1)) {
print " First word was not ";
} else {
print " Second word was not ";
}
print "found in list\n";
return undef;
}
# TESTS
print "Your array has ".$#words." words\n";
print "When 1st word is AFTER 2nd word:\n";
Distance("word7", "word3");
print "When 1st word is BEFORE 2nd word:\n";
Distance("word2", "word5");
print "When 1st word == 2nd word:\n";
Distance("word4", "word4");
print "When 1st word doesn't exist:\n";
Distance("word00", "word6");
print "When 2nd word doesn't exist:\n";
Distance("word1", "word99");
print "When neither 1st or 2nd words exist:\n";
Distance("word00", "word99");
print "When the 1st word is AFTER the 2nd OCCURRENCE of 2nd word:\n";
Distance("word9", "word4", 0, 2);
print "When the 1st word is BEFORE the 2nd OCCURRENCE of the 2nd word:\n";
Distance("word7", "word4", 1, 2);
print "When the 2nd OCCURRENCE of the 2nd word doesn't exist:\n";
Distance("word7", "word99", 0, 2);
print "When the 2nd OCCURRENCE of the 1st word is AFTER the 2nd word:\n";
Distance("word4", "word2", 2, 0);
print "When the 2nd OCCURRENCE of the 1st word is BEFORE the 2nd word:\n";
Distance("word4", "word0", 2, 0);
print "When the 2nd OCCURRENCE of the 1st word exists, but 2nd doesn't:\n";
Distance("word4", "word99", 2, 0);
print "When neither of the 2nd OCCURRENCES of the words exist:\n";
Distance("word00", "word99", 2, 2);
print "Distance between 2nd and 1st OCCURRENCES of the same word:\n";
Distance("word4", "", 2, 1);
возможно дубликат [В Perl, как я могу найти индекс заданного значения в массиве?] (Http://stackoverflow.com/questions/1915746/in-perl-how-can-i -find-the-index-of-a-given-value-in-a-array) – Quentin
Отредактировал вопрос в соответствии с тем, который указан в комментариях ниже. –