I'm working on a calculator for my shell and I cant find anywhere if there is a function that evaulates a string and turns it into a number, for example
"5+6-2" -> 9
Evaluate function?
Moderators: Shine, Insert_witty_name
hi,
will work.
Code: Select all
expression = "5+6-2"
script = "return " .. expression -- convert to LUA script
func, err = loadstring(script)
if err then
-- syntax error
else
result = func() -- now result == 9
end
will work.
Also, the following will work:
Ref: Lua 5 Ref, page 40
Ema, you must have read PIL book well. :)
Hey, guys out there .. hint, hint
Code: Select all
script = "return 5+6-2"
result = assert(loadstring(script))()
Ema, you must have read PIL book well. :)
Hey, guys out there .. hint, hint
Geo Massar
Retired Engineer
Retired Engineer