Similarity(3pm)
π Similarity(3pm) β String::Similarity, bestie edition πΈ
okay so this is Similarity(3pm), a lil' chunk of the Perl User Contributed Documentation, and babes it is SO useful. it's the manual page for String::Similarity, and honestly? it's giving "tiny module, enormous power". let's get into it π
β¨ NAME β¨
String::Similarity β calculate the similarity of two strings π¦
π SYNOPSIS π
use String::Similarity;
$similarity = similarity $string1, $string2;
$similarity = similarity $string1, $string2, $limit;
like. that's it. that's the whole vibe. three lines and you're a string-matching icon π
π DESCRIPTION π
$factor = similarity $string1, $string2, [$limit]
so the similarity function π calculates π the π similarity π index π of its two arguments. and the scale is genuinely so clean:
- a value of
0means the strings are entirely different π like, not even a little bit alike - a value of
1means the strings are identical π they're the same bestie - everything else lies between 0 and 1 and describes the amount of similarity between the strings
how does it do it? roughly, it works by looking at the smallest number of edits needed to change one string into the other. so it's basically measuring "how much surgery would this need before it becomes that other string" π©Ίβ¨
you can also add an optional argument $limit (default 0) that gives the minimum similarity the two strings must satisfy. here's the tea, so listen up π«:
similarity stops analyzing the string as soon as the result drops below the given limit, in which case the result will be invalid but lower than the given $limit. so like, don't trust that number, it's just a "nope, it's below your bar" signal.
why would you want that? because it speeds up the common case of searching for the most similar string from a set β you just specify the maximum similarity found so far π₯ so it can bail early instead of doing all that work for a string that was never gonna win. efficiency queen fr fr π
β οΈ quick reminder bestie: when it bails early, that returned value is invalid. it's lower than $limit, sure, but it is not a real similarity score. use it as a cutoff, not as truth.
π SEE ALSO π
the basic algorithm is described in:
- "An O(ND) Difference Algorithm and its Variations", Eugene Myers, Algorithmica Vol. 1 No. 2, 1986, pp. 251-266 β see especially section 4.2, which describes the variation used below π§
the basic algorithm was independently discovered as described in:
- "Algorithms for Approximate String Matching", E. Ukkonen, Information and Control Vol. 64, 1985, pp. 100-118 β
like okay academic baddies, we see you πβ¨
π AUTHOR π
- Marc Lehmann
<schmorp@schmorp.de> http://home.schmorp.de/
and also, quote unquote: the underlying fstrcmp function was taken from gnu diffutils and modified by Peter Miller <pmiller@agso.gov.au> and Marc Lehmann <schmorp@schmorp.de> π«Ά
aaaand that's the whole page, bestie!! this was perl v5.42.3, dated 2026-08-19, filed under Similarity(3pm) β go forth and compare your strings, the algorithm ate and left no crumbs πβ¨π
π¦ where this page comes from
- ubuntu stonking: libstring-similarity-perl 1.04-3build6 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms