table to math
Posted: Mon Apr 25, 2011 3:05 pm
Lets say if i have a table with "1, 9, +, 5, *, 1", is there some sort of way that I could get them out of the table and then do the maths "19+5*1"?
Code: Select all
local mathstr = table.concat(mathtbl)
local sum = loadstring("return "..mathstr)()
http://pastie.org/1831669slime wrote:easy solution
Depending on how trusted this string is, you might not want to do that. If the string came from another player over the network, the other player could send you something like {"(function() love.update = nil return 7 end)()"}. This seems to be for a calculator that builds the string locally, so it should be fine.slime wrote:Something likemight work.Code: Select all
local mathstr = table.concat(mathtbl) local sum = loadstring("return "..mathstr)()
Yeah it was for a calculator, http://love2d.org/forums/viewtopic.php?f=5&t=2890sharpobject wrote:Depending on how trusted this string is, you might not want to do that. If the string came from another player over the network, the other player could send you something like {"(function() love.update = nil return 7 end)()"}. This seems to be for a calculator that builds the string locally, so it should be fine.