Note that the following split function would fail for a separator of length two or more. So, you wouldn't be able to use it with something like ,:
as a separator.
function split( sInput, sSeparator )
local tReturn = {}
for w in sInput:gmatch( "[^"..sSeparator.."]+" ) do
table.insert( tReturn, w )
end
return tReturn
end
You'll use it as follows:
str = 'PROTOCAL:ROOM:USER:MESSAGE:MESSAGE_ID:TIME_FLOAT'
strlist = split( str, ':' )
Now, for lua-tables, the indexing starts at 1
and not 0
, and you can use table.unpack
to slice small tables. So, you'll have:
a1 = {table.unpack(strlist, 1, 0)} -- empty table
a2 = {table.unpack(strlist, 1, 1)} -- just the first element wrapped in a table
b1 = {table.unpack(strlist, 1, #list)} -- copy of the whole table
b2 = {table.unpack(strlist, 2, #list)} -- everything except first element
c = strlist[1]
(table.unpack
works in Lua 5.2, it is just unpack
in Lua 5.1)
For larger tables you may need to write your own shallow table copy function.