Difference between revisions of "Talk:binser"
Line 1: | Line 1: | ||
Is here any inbuilt serialize function? --[[User:Darkfrei|Darkfrei]] ([[User talk:Darkfrei|talk]]) 20:45, 20 January 2021 (CET) | Is here any inbuilt serialize function? --[[User:Darkfrei|Darkfrei]] ([[User talk:Darkfrei|talk]]) 20:45, 20 January 2021 (CET) | ||
+ | |||
+ | |||
+ | For example (my code) | ||
+ | <source lang="lua"> | ||
+ | serialize = function (tabl) | ||
+ | local str = '' | ||
+ | for i, v in pairs (tabl) do | ||
+ | local pr = (type(i)=="string") and i..'=' or '' | ||
+ | if type (v) == "table" then | ||
+ | str = str .. pr..'{'..serialize(v)..'},' | ||
+ | elseif type (v) == "string" then | ||
+ | str = str .. pr..'"'..tostring(v)..'",' | ||
+ | else | ||
+ | str = str .. pr..tostring(v)..',' | ||
+ | end | ||
+ | end | ||
+ | str = str:sub(1, -2) -- remove last comma | ||
+ | return str | ||
+ | end | ||
+ | |||
+ | local a = {1,2,3, a='b', c={"d",e="e"},d={{1},{2}}, f={}} | ||
+ | print (serialize(a)) -- 1,2,3,d={{1},{2}},f={},a="b",c={"d",e="e"} | ||
+ | </source> | ||
+ | --[[User:Darkfrei|Darkfrei]] ([[User talk:Darkfrei|talk]]) 17:57, 21 February 2021 (CET) |
Revision as of 16:57, 21 February 2021
Is here any inbuilt serialize function? --Darkfrei (talk) 20:45, 20 January 2021 (CET)
For example (my code)
serialize = function (tabl)
local str = ''
for i, v in pairs (tabl) do
local pr = (type(i)=="string") and i..'=' or ''
if type (v) == "table" then
str = str .. pr..'{'..serialize(v)..'},'
elseif type (v) == "string" then
str = str .. pr..'"'..tostring(v)..'",'
else
str = str .. pr..tostring(v)..','
end
end
str = str:sub(1, -2) -- remove last comma
return str
end
local a = {1,2,3, a='b', c={"d",e="e"},d={{1},{2}}, f={}}
print (serialize(a)) -- 1,2,3,d={{1},{2}},f={},a="b",c={"d",e="e"}