在Lua中,如何将表用作varargs(...)? [英] in Lua, how can I use a table as varargs (...)?

查看:56
本文介绍了在Lua中,如何将表用作varargs(...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 as another argument.

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])

推荐答案

使用较新版本的Lua在表中放置了 unpack函数模块:

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

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

这不能直接回答您的问题,但正如 lhf 指出的那样,以下方法效率更高:

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

local myString = table.concat(myTable, ' ')

这篇关于在Lua中,如何将表用作varargs(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆