in reply to Tuning an approximate match to prefer closer lengths

using Text::Levenshtein's distance function, I get source1 as the best match.
use Text::Levenshtein qw(distance); $target = 'source'; $best=undef; for (qw(donor_dedupe_source indicted_video_source source1)) { $dist = distance($target,$_); if ($best == undef || $dist < $best) { $best = $dist; $bestword = $_; } } print "Best word was $bestword with an edit distance of $best\n";
output: Best word was source1 with an edit distance of 1

update: changed default value of $best to prevent perfect matches from being lost