如何在 Lua 中存储特定的值并检索拼接?

我有四个函数。

每个函数都有执行时间。

它将在表中存储一些不同的值。

当执行输入函数时,它就会逐个检索。

无论哪个函数的数据存储在表中。

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("逐个检索",v)
    end
end
one()
two()
enter()

输出:1 2(这是逐个检索的顺序)

我想要的输出是:12

如果我下次按不同的顺序执行函数,则

two()
one()
enter()

输出:2 1(这是逐个检索的顺序)

我想要的输出是:21

如果我下次执行

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

我想要的输出是 234

是否可能编写代码。

请任何人帮忙

点赞
用户2858170
用户2858170

首先,覆盖 table 是不明智的做法。

如果你想按照特定的顺序获取表元素,你不应该使用 pairs 迭代器,因为它使用 next 枚举表键的顺序是不确定的。

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

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

2019-03-08 06:50:46