perl String::Approx on Arrays
I'm using String::Approx to find the most alike match for a two-item array out of a list of others. I was pleasantly surprised to find that you can use amatch()
to compare an array to an array although that feature is not documented; I was prepared to write my own function to do that. I was further surprised to see that the order of the elements doesn't matter. But, even though amatch()
works flawlessly, I'm having a difficult time with adist()
. Consider the following program:
#! /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 o开发者_如何学运维f $dist \n";
}
And here's what it outputs:
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
So, it appears to be picking the right answers (it ignored ['giraffe', 'elephant']
and ['hawk', 'shark']
), but it can't tell me the distance. The end goal is to order the matches by distance and pick the one most like @matchstr
. Is amatch()
actually working as well as I think it is, or am I just using too simple of input? Why isn't amatch()
working?
You can not pass an array as the first argument to amatch or adist and have it work as you are expecting.
Arrays are unpacked into lists, so what amatch sees is something like amatch( 'cat', 'dog', 'rat', 'hog' )
which is of course not what you intended.
You will have to create new versions of amatch and adist that support an array reference as the first argument. You will then need to call the subs as my_amatch(\@matchstr, @$_)
amatch isnt' doing what you think it is.
If you change qw(cat dog) to qw(cat zzz) you get the same results.
Then, if you change "hawk", "shark" to "hawk", "zzz" you still get the same results.
It looks like it's only doing the comparison with "cat".
精彩评论