Post your best Lua script examples here

User avatar
By gerardwr
#3903
zeroday wrote:well I have been follow you posts everyday.
but I have no solutions in lua way, me a lua newbie too. :oops:
the first thing came into my head, is to make a api in C to do this,
take a lua statement, run it, and return a string result... but this is ugly...


Nice that you follow my struggle, thanks for your help.

Yep, an API in C, that's ugly ;) , and too complicated for me!

I'm glad that I'm not missing anything obvious here.

But wait……

I think I have found A solution in the elua -httpd webserver http://wiki.eluaproject.net/lhttpd)

There's a docode function in it, that works in nodeMco too. I takes a Lua statement as input in a string, executes the loadstring of the string, and returns the result in a string. It works by a temporary replacement of the print function by a function that prints to string. Neat huh!

I did a quick test:
Code: Select all> s=[[print(node.heap())]]
> a=docode(s)
> =a
16384
> s='for i=1,3 do print(i) end'
> out=docode(s)
> =out
1
2
3

> s='now introduce a lua error for i=1,3 do print(i) end'
> out=docode(s)
> =out
>>> Invalid Lua code <<<

>


My problem can be solved in this way, I guess.
User avatar
By zeroday
#3909
gerardwr wrote:
zeroday wrote:well I have been follow you posts everyday.
but I have no solutions in lua way, me a lua newbie too. :oops:
the first thing came into my head, is to make a api in C to do this,
take a lua statement, run it, and return a string result... but this is ugly...


Nice that you follow my struggle, thanks for your help.

Yep, an API in C, that's ugly ;) , and too complicated for me!

I'm glad that I'm not missing anything obvious here.

But wait……

I think I have found A solution in the elua -httpd webserver http://wiki.eluaproject.net/lhttpd)

There's a docode function in it, that works in nodeMco too. I takes a Lua statement as input in a string, executes the loadstring of the string, and returns the result in a string. It works by a temporary replacement of the print function by a function that prints to string. Neat huh!

I did a quick test:
Code: Select all> s=[[print(node.heap())]]
> a=docode(s)
> =a
16384
> s='for i=1,3 do print(i) end'
> out=docode(s)
> =out
1
2
3

> s='now introduce a lua error for i=1,3 do print(i) end'
> out=docode(s)
> =out
>>> Invalid Lua code <<<

>


My problem can be solved in this way, I guess.


Neat!! That's the true power of lua.
User avatar
By alonewolfx2
#3912 @gerardwr can you catch requested headers in your webserver?
Code: Select allAccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive