lua脚本分解字符串

--local str = "文字45 文字 789 文们adsd45 文字 wowo 文字 文字 wowo我们 wowo456 wiwo 465我们 456sdf 45 45我们adsd45"
local s1="温馨提醒:现在离2010汇锦集团表面处理技术研讨会还有1天,报到时间为6报月19日14:00-20:00:who are you"--模板
--local s2="一aA 馨提醒二b B文字b abc文字 "
--local s2="一aA 馨提醒二b B文字b ab现在离2010汇锦集团表面处理技术研c文字 "
--local s2="一aA 馨提醒二b B文字b ab现在离2010汇锦集团表面处理技术研c文字6报月19日14:00-20:00:who are yo "
local s2="温馨理技术研讨时间为6报月19日14:00-20:00:B文字b are you提醒:现在离2010汇锦集团表会还有1天,报到面处"
--myTable=setmetatable({a1="ss"},{key2="value2"},{a1="sdd"})
local pointer=0
--local slen=string.len(str)
--print(slen)

local pos=1;
local myTable={}
local myTable1={}
local sName=""
local function Put(myT,sName)  --添加内容到table中统计字符出现的个数
local i=1
if(myT[sName]~=nil) then
myT[sName]=i+1
else

myT[sName]=i
end
--print(sName..":"..myT[sName])
end
local function CompareString(str,myT)  --分解字符串

local slen=string.len(str)
local m=1;
while(true)
do
if(m>slen) then
if(pos<=m-1) then
--print(m .. ":" ..string.sub(str,pos,m-1))
sName=string.sub(str,pos,m-1)
Put(myT,sName)
end
--print("m" .. ":" .. m)
break
end
local bi=string.byte(str,m,m)
if(bi>127) then
if(pos<=m-1) then --7<=10
--print(m .. ":" .. string.sub(str,pos,m-1)) --11:7-9
sName=string.sub(str,pos,m-1)
Put(myT,sName)
end
--print(m .. ":" .. string.sub(str,m,m+1)) --6 4-7
sName=string.sub(str,m,m+1)
Put(myT,sName)
pos=m+2 --pos=6
m=m+2 --m=8
elseif(bi==32) then
if(pos<=m-1) then --6<=8
--print(m .. ":" .. string.sub(str,pos,m-1))--9: 6-8
sName=string.sub(str,pos,m-1)
Put(myT,sName)
end
pos=m+1 --pos=7
m=m+1 --m=10
else
m=m+1 --m=11
end

--local sss=string1..m
--local sssss=string.gsub("myTable.sss","sss",sss,3)
--myTable[sName]="1"
--sssss=sName
end
end
CompareString(s1,myTable)
CompareString(s2,myTable1)

-- 输出计算结果
local molecule=0
local Denominator1=0
local Denominator2=0
--print("-----------------------------------------------")
for key, value in pairs(myTable) do
--print(key,value)
--print(myTable[key])
--print(myTable1[key])
if(myTable1[key]==nil) then
else
molecule=molecule+myTable[key]*myTable1[key]
end
Denominator1=Denominator1+math.pow(myTable[key],2)
end
--print("-----------------------------------------------")
for key1,value1 in pairs(myTable1) do
--print(key1,value1)
Denominator2=Denominator2+math.pow(myTable1[key1],2)
end
--print("---------------------")
--print(molecule) --18
--print(math.sqrt(Denominator1)) --6.77
--print(math.sqrt(Denominator2)) --6
local q1=math.sqrt(Denominator1)
local q2=math.sqrt(Denominator2)
local sum= molecule/(math.sqrt(Denominator1)*math.sqrt(Denominator2))
redis.call("SET", "Id", sum)
return redis.call("GET", "Id")

--return molecule


-- 使用 .. 进行字符串连接

--[[myTables=setmetatable({1,2,3},{
_add=function(myTables,newtable)
for i=1,table_maxn(newtable) do
table.insert(myTables,table_maxn(myTables)+1,newtable[i])
end
return myTables
end
})
secondtable={4,5,6}
myTables=myTables+secondtable
for k,v in ipairs(myTables) do
print(k,v)
end
--]]
--[[for key,value in pairs(myTable) do
print(key,value)
end --]]
--print(myTable["文"])

原文地址:https://www.cnblogs.com/zwyAndDong/p/7416412.html