This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pofferman's profile picture

[Solved] Table math

Started by pofferman, 29 August 2015 - 04:28 PM
pofferman #1
Posted 29 August 2015 - 06:28 PM
Hey guys, so i wonder if you could do math with a table like this


local temp = {
"(",
"10",
"+",
"5",
")",
"*",
"2"
}
print(table.concat(temp))

The code above outputs "(10+5)*2"
But i wonder if somehow i could make it do the math, so it outputs "30"?
Edited on 05 September 2015 - 09:12 PM
KingofGamesYami #2
Posted 29 August 2015 - 06:42 PM
Change the last line to this:


print( loadstring( "return " .. table.concat( temp ) )() )

To clarify, loadstring turns a string into a function. I added "return " to the beginning of the operation so that the created function would return the result. I then called the function, which returned the result, and the result was printed by print.
pofferman #3
Posted 29 August 2015 - 07:02 PM
I thank u so mutch:D
that really worked :)/> ty ty ty :D/>