有没有办法使用table.concat的arg 2值来表示当前表索引?
eg:
t = {}
t[1] = "a"
t[2] = "b"
t[3] = "c"
X = table.concat(t,"\n")
表 concat (X) 的所需输出:
"1 a\n2 b\n3 c\n"
简单的回答:不。
table.concat
是非常基本的东西,而且非常快。
所以无论如何你应该循环执行它。
如果你想避免过多的字符串连接,你可以这样做:
function concatIndexed(tab,template)
template = template or '%d %s\n'
local tt = {}
for k,v in ipairs(tab) do
tt[#tt+1]=template:format(k,v)
end
return table.concat(tt)
end
X = concatIndexed(t) -- and optionally specify a certain per item format
Y = concatIndexed(t,'custom format %3d %s\n')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)