function getVarCache(play,varName,key)
local str = getplaydef(play, varName)
local result = {}
for k, v in string.gmatch(str, "([^=]+)=([^,]+)") do
k = k:gsub(",", "")
result[k] = v
end
return result[tostring(key)] or ""
end
于是我写了一个通过正则匹配的方式来修改和读取键值对:
function getVarCache(play, varName, key)
local str = getplaydef(play, varName)
local newstr = "," .. str .. ","
local pattern = "," .. key .. "=([^,]+),"
local matchValue = newstr:match(pattern)
if matchValue then
return matchValue
else
return ""
end
end
通过系列化方式来存键值对: -- 系列化算法存键值对
function updateVarCache(play, varName, key, value)
local str = getplaydef(play, varName)
local result = {}
for k, v in string.gmatch(str, "([^=]+)=([^,]+)") do
k = k:gsub(",", "")
result[k] = v
end
if value ~= nil and value ~= "" then
result[tostring(key)] = tostring(value)
else
result[tostring(key)] = nil
end
local updatedStr = ""
for k, v in pairs(result) do
updatedStr = updatedStr .. k .. "=" .. v .. ","
end
updatedStr = string.sub(updatedStr, 1, -2) -- 去掉最后的逗号