Post your best Lua script examples here

User avatar
By gerardwr
#3552 Need some help from the Lua experts here.

For my web browser example (http://www.esp8266.com/viewtopic.php?f=19&t=611&start=40) I need a way to execute a Lua statement thats in a string variable AND return the result in a string. Even better, some kind of multiline string as input, and a multistring as output.

I had a look at loadstring ( http://www.gammon.com.au/scripts/doc.php?lua=assert). That executes my string nicely.
Code: Select all> s=assert (loadstring ("print 'hello, world' print(tmr.now())")) ()
hello, world
344158447
> =s
nil
>


But….. the result is "just" printed, and not assigned to the variable s as I had hoped :?

I had a QUICK look to node.input, knowing that it would not return a result on the command line, I naively did anyway. Yep, no dice:
Code: Select all> node.input("print('test')")
> s=node.input("print('test')")
> =s
nil
>


Just to be clear : if the Lua statement is executed OK, I want the result of the code returned into a variable. If the Lua statement contains an error I would like the error-line returned into a string variable (ref. the error line in the code below):
Code: Select all> s=assert (loadstring ("rint 'hello, world' print(tmr.now())")) ()
lua: [string "rint 'hello, world' print(tmr.now())"]:1: attempt to call global 'rint' (a nil value)
>


Put my nose in the right direction, please?
User avatar
By gerardwr
#3594 Thanks zero day, I will try the "high road" using node.input and node.output first.

The tostring() code is usable for function that return a numerical value but I need code that interprets any Lua statement, and I image that tostring is not capable to do that.

Can't test right now, my HW is on the fritz ;)