我正在处理表,我想再次将表转换为字符串。 首先,我这样做:
local function TableToString(t)
return "{"..table.concat(t,",").."}"
end
local MyTable = {4,5,21,7}
print(TableToString(MyTable))--It prints {4,5,21,7}
但是后来我尝试了一个包含键和值的表:
local MyTable2 = {Key1=5819,Key2=28194,Key3=1888}
print(TableToString(MyTable2))--It prints " "
所以我做到了:
local function TableToString(t)
if #t == 0 then
TempTab={}
for i,v in next,t do
table.insert(TempTab,i.."="..v)
end
t=TempTab
end
return "{"..table.concat(t,",").."}"
end
local MyTable = {4,5,21,7}
local MyTable2 = {Key1=5819,Key2=28194,Key3=1888}
print(TableToString(MyTable)
)--It prints {4,5,21,7}
print(TableToString(MyTable2))--It prints {Key1=5819,Key2=28194,Key3=1888}
但是,然后是一个数组数组
local MyTable3 = {Key1={Subkey1={}},Key2={Subkey2={}},Key3={Subkey3={}}}
print(TableToString(MyTable3))--[Error]attempt to concatenate local 'v' (a table value)
现在我不知道该怎么办。 已修复
local Is=setmetatable({},{
__index=function(t,k)
compare = function(to)
if type(to):lower() == k:lower() then
return true
end
return false
end
if k:lower() == "matrix" then
compare = function (to)
if type(to) == "table" and #to == 0 then
return true
end
return false
end
end
return compare
end
})
function TTS(t)
if Is.Table(t) then
local TempTab={}
if not Is.matrix(t) then
return "{"..table.concat(t,",").."}"
else
for i,v in pairs(t) do
if not Is.Table(v) then
table.insert(TempTab,i.."="..v)
else
table.insert(TempTab,i.."="..TTS(v))
end
end
return "{"..table.concat(TempTab,",").."}"
end
end
end
local MyTable3 = {Key1={Subkey1={}},Key2={Subkey2={}},Key3={Subkey3={}}}
print(TableToString(MyTable3))--Prints {Key1={Subkey1={}},Key2={Subkey2={}},Key3={Subkey3={}}}
有一个功能是有人要使用它;)