Unfortunately, in LUA, there is no inbuilt string splitting function, which is very inconvenient. However, you could use string.gmatch to begin a regular expression matching and the following is a short and neat alternative.
A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings. This usually requires string parsing.
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
The following tests it:
s = split(io.read(), ",")
for key, value in pairs(s) do
print(key..'='..value)
end
Unlike other programming language, the string concatenation is made possible by double dots “..”.
–EOF (The Ultimate Computing & Technology Blog) —
154 wordsLast Post: Coding Exercise - 1. Life, the Universe, and Everything - Learning LUA programming language
Next Post: Fasthosts Disable My website because WordPress sending too many emails due to spam comments