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 0 means the strings are entirely different πŸ’€ like, not even a little bit alike
  • a value of 1 means 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 πŸ‘‘

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