如何在Lua中存储特定值并检索串联?

时间:2019-03-08 06:17:59

标签: lua

我有四个功能。
每个函数的执行时间。
它将在表中存储一些不同的值。
当执行Enter函数时,它将进行检索。
一对一。
表中存储的任何功能数据。

table={} 
function one()
    table.one="1"
end 
function two()
    table.two="2"
end 
function three() 
    table.three="3"
end 
function four() 
    table.four="4" 
end
function enter() 
    for i,v in pairs(table)do 
        print("on by one",v)
    end
end 
one() 
two() 
enter() 

输出:1 2(顺序一一)
我想要这样的输出:12
如果我下次执行不同顺序的功能,那么

two()
one()
enter() 

输出:2 1(顺序一一)
我想要这样的输出:21
如果我下次执行

two() 
three()
four()
enter()

我想要这样的输出234
可以写代码吗?
请帮助任何人

1 个答案:

答案 0 :(得分:1)

首先,覆盖table并不是一个好主意。

如果您有兴趣按特定顺序获取表元素,则不应使用pairs迭代器,因为它会利用next来枚举未指定顺序的表键。

local digits = {}
function one()
  table.insert(digits, 1)
end
function enter()
  print(table.concat(digits))
  digits = {}
end

请注意,这仅适用于字符串或数字值。