sub mkamatch { # (pattern,kmismatches) use integer; my $p = shift; my $k = shift; my $m = length($p); $k = (10 * $m) / 100 + 1 unless defined $k; my @p2 = map { 1 << $_ } 0 .. 31; my @t = (0) x 256; # alphabet size my $i = -1; $t[$_] |= $p2[++$i] for (unpack("C*",$p)); my (@s,@r); $#s = $#r = $k; for ($r[0] = 0, my $i = 0; $i < $k; $i++){ $r[$i+1] = $r[$i] | $p2[$i]; } sub { my $key = shift; my $n = length($key); my $mb = $p2[$m-1]; my $ret = -1; my @str = unpack("C*",$key); for ($s[0] = 0, my $i = 0; $i < $n; $i++){ $s[0] = (($s[0] << 1) | 1) & (my $tc = $t[$str[$i]]); for (my $j = 1; $j <= $k; $j++){ $s[$j] = (((($r[$j] << 1) & $tc) | (($r[$j-1] | $s[$j- +1]) << 1)) | $r[$j-1]) | 1; } $ret = $i > $m ? $i - $m : 0, last if $s[$k] & $mb; @r = @s; } $ret; } }

In reply to Approximate Matching w/o C by nothingmuch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.