How can I find out if two strings are mostly equal (in perl)?
-
05-10-2019 - |
Question
I have a string that I want to compare against an array of strings, and return the array element that most closely matches.
I can write a sliding correlator that counts the number of matching characters at each step and returns the max correlation. But is there a better way?
For example:
control_string = drv_probability_1_max
List:
burst_period_min/max
duty_cycle_min/max
probablility_0_min/max
probablility_1_min/max
Where ideally it returns "probablility_1_min/max"
Solution
use String::Approx
;
OTHER TIPS
You might be looking for the String::Similarity
module.
Take a look at Text::Levenshtein or List::Compare (String::Approx is the same method, but a more complete package)
use strict; use warnings;
use Text::Levenshtein qw(distance);
my $ctl = "drv_probability_1_max";
my @list=qw|
burst_period_min/max
duty_cycle_min/max
probablility_0_min/max
probablility_1_min/max
|;
my @dist=distance($ctl,@list);
print "Levenshtein distances: @dist\n";
my $idmin=0;
$dist[$idmin] < $dist[$_] or $idmin = $_ for 1..$#dist;
print "\"$list[$idmin]\" seems the closest...\n\n\n";
Output:
Levenshtein distances: 16 16 10 9
"probablility_1_min/max" seems the closest...
Read more about Levenshtein Distance or implement a Perl algorithm directly..