I was searching HTML looking for items that could be separated by, for example, an unknown amount of tags. The code returns two values to show how good the match was.
The items to search for are pushed into @names.
Ideally $success should be 100 i.e. all elements were
matched and $goodness should be 100 i.e. the length of the
elements was equal to the length of the matched portion of
the string because there were few unwanted characters
$matched = 0;
$pmin = -1;
$pmax = 0;
foreach $namepart (@names) {
if (/$namepart/gc) {
$matched++;
$pmin = pos($_) - length($namepart) if ($pmin == -1);
$pmax = pos($_);
}
}
$success = int($matched/($#names+1) * 100);
$goodness = int(($pmax - $pmin) / $len * 100);
$goodness = 0 if (!$success);