Posted 05 July 2015 - 12:50 AM
Is there a way to take something like "data1|data2" and convert it to two different variables along the | symbol?
Is there a way to take something like "data1|data2" and convert it to two different variables along the | symbol?
function separate(data)
local matches = {}
local a = 1
for token in data:gmatch("[^|]+") do --iterate over any value that is NOT '|' (gmatch code []= custom pattern, ^=NOT |=delimiter we are using +=as many matches as possible)
matches[a] = token
a=a+1
end
print(table.unpack(matches))
end
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table, cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
message = "hello:world"
msgTable = split(message,"\:")
print(msgTable[1])
print(msgTable[2])