gpt4 book ai didi

lua - 在 Lua 中,如何将表用作可变参数(...)?

转载 作者:行者123 更新时间:2023-12-04 02:46:58 26 4
gpt4 key购买 nike

strjoin 接受一个字符串,然后是可变数量的参数。我正在寻找一种方法来获取带有可变数量参数的表格,并将表格中的每个项目用作另一个参数。

local myTable = {
'a',
'b',
'c',
}
-- This is what I want except that I don't want to hard code
-- a specific number of parameters

local myString = strjoin(' ', myTable[1], myTable[2], myTable[3])

最佳答案

使用 unpack功能:

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

较新版本的 Lua 放置了 unpack function in the table module :
local myString = strjoin(' ', table.unpack(myTable))

这不会直接回答您的问题,而是作为 lhf指出,以下更有效:
local myString = table.concat(myTable, ' ')

关于lua - 在 Lua 中,如何将表用作可变参数(...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3473770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com