Was working with threads and trying to get a faster way to convert a string to a table.
My first approcah was:
Code: Select all
function extractPoints( stringData ) -- string => point table{x,y,z}
local temp = stringData:split() -- p1x,p1y,p1z,... => temp{p1x,p1y,p1z, ... }
local reconstructedTable = {}
local field = 1
for i = 1, #temp, 3 do -- temp{p1x,p1y,p1z, ... } => table{{p1x,p1y,p1z},{p2x,p2y,p2z}, ... }
reconstructedTable[ field ] = { temp[i], temp[i+1], temp[i+2] }
field = field + 1
end
return reconstructedTable
end
function string:split( sep ) -- parces string 'x,y,z' into x y z - default separator == comma
local sep, fields = sep or ",", {}
local pattern = string.format( "([^%s]+)", sep )
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
Code: Select all
function string2table( string )
assert( loadstring( string ))()
end
Any ideas why?