1
我目前正在编写一个Lua脚本。在那里,我想有一个变量名称,它与一个越来越多的数字连接起来。如何创建与递增数字连接的变量名称
例子:Q0001,Q0002,Q0003,...,Q9999
我给这下面的脚本是:
local rnd = math.random (0,9999)
local Text = ""
print(rnd)
if rnd > 0 and rnd < 10 then
--Add Nulls before Number and the "Q"
Text = Q000 .. rnd
elseif rnd >= 10 and rnd < 100 then
--Add Nulls before Number and the "Q"
Text = Q00 .. rnd
elseif rnd >= 100 and rnd < 1000 then
--Add Null before Number and the "Q"
Text = Q0 .. rnd
elseif rnd >= 1000 then
--Add "Q"
Text = Q .. rnd
end
print(Text)
按道理我把这个变成一个功能,因为它只是一部分的我PROGRAMM。在程序后期,我喜欢用变量获取信息,因为变量Q###
的乘积是我编程的表格。我的第二个想法来解决这个问题是将其转换为文本,但是我不知道如何将其转换为声明。
编辑04/04/15 19:17:太使它更清楚。我希望该文本在我之前设置的表格的脚本结尾之后站立。所以我可以说Text.Name
例如
或者只是使用普通数组。 – lhf 2015-04-04 11:16:01
好吧,我google了string.format函数,但只有一个列表与argruments。所以我不太明白你在这里向我解释了什么。我在控制台中测试了你的例子后,名字Q ###被发布了,但是应该没有,因为我还没有声明变量。我得到以下错误: – 2015-04-04 17:14:59
org.luaj.vm2.LuaError:C:\ ...'FileName'.lua:46表索引预计,得到表 堆栈回溯: \t C:\ ...'FileName' .lua:46:主要区块 \t [Java]:in? \t在org.luaj.vm2.LuaValue.typerror(未知来源) \t在org.luaj.vm2.LuaTable.set(未知来源) \t在org.luaj.vm2.LuaClosure.execute(未知来源) \t在org.luaj.vm2.LuaClosure.onInvoke(未知来源) \t在org.luaj.vm2.LuaClosure.invoke(未知来源) \t在lua.processScript(未知来源) \t在lua.main(未知来源) – 2015-04-04 17:15:48