Advertisement
boatbomber

Soundex Lua

Dec 31st, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.56 KB | None | 0 0
  1. local d, digits, alpha = '01230120022455012623010202', {}, string.byte("A")
  2. string.gsub(d, ".", function(c)
  3.     digits[string.char(alpha)] = c
  4.     alpha = alpha + 1
  5. end)
  6.  
  7. local function Soundex(w)
  8.     local res = {}
  9.    
  10.     for c in string.gmatch(string.upper(w), ".") do
  11.         local d = digits[c]
  12.         if d then
  13.             if #res==0 then
  14.                 res[1] =  c
  15.             elseif #res==1 or d~= res[#res] then
  16.                 res[1+#res] = d
  17.             end      
  18.         end    
  19.     end
  20.     if #res == 0 then
  21.         return '0000'
  22.     else
  23.         res = string.gsub(table.concat(res), "0", "")
  24.         return string.sub(res .. '0000',1,4)
  25.     end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement