Lua: Executing a string and storing the output of the command in a variable

后端 未结 2 571
时光说笑
时光说笑 2021-01-25 22:50

I\'ve got a Lua script that receives a function call in a string. I need to execute that call and retrieve the output as a string in a variable so I can later send it somewhere.

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 23:40

    I just found a way to do what I wanted, but I'd still like to know if you guys can find any flaw/better way to do it, since I'm very new to Lua:

        > s = "json.encode('{1:1, 2:3, 5:8}')"
        > s2 = "return("..s..")"
        > ret = assert(loadstring(s2))()
        > print(ret)
        "{1:1, 2:3, 5:8}"
    

提交回复
热议问题