Created
August 24, 2020 06:15
-
-
Save sofish/19cce9f29a58584f389198edc39d332b to your computer and use it in GitHub Desktop.
read str and parse it as table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function s2t(str) | |
local t = {} | |
local l = string.len(str) | |
local key = '' | |
local val = '' | |
local oe = 0 | |
for i = 1, l do | |
local s = string.sub(str, i, i) | |
if s ~= ' ' then | |
if oe % 2 ~= 1 then | |
key = key..s | |
else | |
val = val..s | |
end | |
end | |
if s == ' ' or i == l then | |
oe = oe + 1 | |
if oe % 2 == 0 then | |
t[key] = val | |
key = '' | |
val = '' | |
end | |
end | |
end | |
return t | |
end | |
local str = 'world000 -2000 he-llo-0 1' | |
print(s2t(str)) -- { ["he-llo-0"] = 1, ["world000"] = -2000, } |
Author
sofish
commented
Aug 24, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment