我正在使用String::Approx从其他列表中找到最相似的匹配项。我惊喜地发现您可以使用amatch()
数组与数组进行比较,尽管该功能没有记录在案;我准备编写自己的函数来做到这一点。我更惊讶地发现元素的顺序并不重要。但是,即使amatch()
工作完美无缺,我在adist()
. 考虑以下程序:
#! /usr/bin/perl
use String::Approx qw (amatch adist);
@matches = qw();
%matchhash = qw();
@matchstr = qw(cat dog);
@poss = (['rat', 'hog'],
['gnat', 'frog'],
['giraffe', 'elephant'],
['dig', 'bat'],
['catatonic', 'doggone'],
['care', 'dog'],
['care', 'ding'],
['hawk', 'shark']);
@matches = grep { amatch (@matchstr, @$_) } @poss;
foreach $k (@matches)
{
$dist = adist( @matchstr, @$k );
print "@matchstr has a difference from @$k of $dist \n";
}
这是它的输出:
cat dog has a difference from rat hog of 3
cat dog has a difference from gnat frog of 3
cat dog has a difference from dig bat of 3
cat dog has a difference from catatonic doggone of 3
cat dog has a difference from care dog of 3
cat dog has a difference from care ding of 3
所以,它似乎选择了正确的答案(它忽略了['giraffe', 'elephant']
and ['hawk', 'shark']
),但它不能告诉我距离。最终目标是按距离对比赛进行排序并选择最喜欢的比赛@matchstr
。实际上是否amatch()
像我认为的那样工作,或者我只是使用了过于简单的输入?为什么不amatch()
工作?