Lua table.concat [英] Lua table.concat

查看:124
本文介绍了Lua table.concat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用table.concat的arg 2值来表示当前表索引?

Is there a way to use the arg 2 value of table.concat to represent the current table index?

例如:

 t = {}
 t[1] = "a"
 t[2] = "b"
 t[3] = "c"

 X = table.concat(t,"\n")

表concat(X)的所需输出:

desired output of table concat (X):

 "1 a\n2 b\n3 c\n"

推荐答案

简单答案:否.

table.concat确实非常基础,而且非常快.

table.concat is something really basic, and really fast.

所以您应该以循环方式进行.

So you should do it in a loop anyhow.

如果要避免过多的字符串连接,可以执行以下操作:

If you want to avoid excessive string concatenation you can do:

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

这篇关于Lua table.concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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