I have added a couple of iterators to the Code Snippets.
This afternoon I took a look at Mikes Soundex routine and tried a little experiment, using a Function factory to return a function at the same time as setting up it's parameters as local variables.
I suspect it's slightly quicker than the other version, but also has the advantage of being self contained with no global variables.
Any thoughts?
Code: Select all
--[[
@Title: Soundex Calculator
@Author: Jane Taubman
@Version: 1.0
@LastUpdated: October 2012
@Description: Function to Convert any Name to Soundex as per http://en.wikipedia.org/wiki/Soundex
and http://creativyst.com/Doc/Articles/SoundEx1/SoundEx1.htm#SoundExAndCensus
]]
function newSoundEx()
local TblSoundex = {} -- Soundex dictionary of previously coded Names & Places
local TblCodeNum = { -- Soundex code number table
A=0,E=0,I=0,O=0,U=0,Y=0, -- H=0,W=0, -- H & W are ignored
B=1,F=1,P=1,V=1,
C=2,G=2,J=2,K=2,Q=2,S=2,X=2,Z=2,
D=3,T=3,
L=4,
M=5,N=5,
R=6
}
return function (strAnyName)
strAnyName = string.upper(strAnyName:gsub('[^%a]','')) -- Make name upper case letters only
if strAnyName == '' then return 'Z000' end
local strSoundex = TblSoundex[strAnyName] -- If already coded then return previous Soundex code
if strSoundex then return strSoundex end
local strSoundex = string.sub(strAnyName,1,1) -- Soundex starts with initial letter
local tblCodeNum = TblCodeNum -- Local reference to Global table is faster
local strLastNum = tblCodeNum[strSoundex] -- Set initial Soundex code number
for i = 2, string.len(strAnyName) do
local strCodeNum = tblCodeNum[string.sub(strAnyName,i,i)] -- Step through Soundex code of each subsequent letter
if strCodeNum then
if strCodeNum > 0 and strCodeNum ~= strLastNum then -- Not a vowel nor same as Soundex preceeding code
strSoundex = strSoundex..strCodeNum -- So append Soundex code until 4 chars long
if string.len(strSoundex) == 4 then
TblSoundex[strAnyName] = strSoundex -- Save code for future quick lookup
return strSoundex
end
end
strLastNum = strCodeNum -- Save as Soundex preceeding code, unless H or W
end
end
strSoundex = string.sub(strSoundex..'0000',1,4) -- Pad code with zeroes to 4 chars long
TblSoundex[strAnyName] = strSoundex -- Save code for future quick lookup
return strSoundex
end -- function StrSoundex
end -- function newSoundEx
local strSoundex = newSoundEx() -- Create strSoundex Function.
print(os.clock())
for i = 1,10000 do
for _,v in ipairs({'Mullins','Smith','Jones','Smith'}) do
local s = (strSoundex(v))
end
end
print(os.clock())