in Lua, how can I use a table as varargs (…)?

后端 未结 2 440
孤独总比滥情好
孤独总比滥情好 2020-12-17 19:40

strjoin accepts one string and then a variable number of arguments. I\'m looking for a way to take a table with a variable number of arguments and use each item in the table

相关标签:
2条回答
  • 2020-12-17 20:19

    Use the unpack function:

    local myString = strjoin(' ', unpack(myTable))
    

    Newer versions of Lua place the unpack function in the table module:

    local myString = strjoin(' ', table.unpack(myTable))
    

    This doesn't answer your question directly, but as lhf pointed out, the following is much more efficient:

    local myString = table.concat(myTable, ' ')
    
    0 讨论(0)
  • 2020-12-17 20:32

    Use table.concat instead of strjoin.

    0 讨论(0)
提交回复
热议问题