Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 将元素插入到其他表中的表中_Lua - Fatal编程技术网

Lua 将元素插入到其他表中的表中

Lua 将元素插入到其他表中的表中,lua,Lua,我需要帮助来了解如何使用table.insert,以及如何将元素插入到一个表中,而另一个表中。这就是我所拥有的: Table = {} --Main table function InsertNewValues() local TIME = --Any value, string or integer local SIGNAL = --Any value, string or integer table.insert(Table, {TIME, SIGNAL}) end 好的,这允许我在

我需要帮助来了解如何使用table.insert,以及如何将元素插入到一个表中,而另一个表中。这就是我所拥有的:

Table = {} --Main table

function InsertNewValues()

local TIME = --Any value, string or integer
local SIGNAL = --Any value, string or integer
table.insert(Table, {TIME, SIGNAL})

end
好的,这允许我在每次调用该函数时插入时间值和信号值,因此该表将具有:

Table[1][1] = TIME
Table[1][2] = SINGAL
...
Table[...][1] = TIME
Table[...][2] = SIGNAL
但是。。。我需要将时间和信号的值插入到表“table”中的另一个表中,该表作为键引用这些值。。。时间和信号

因此,结果表如下所示:

+Table
|
+-[1]othertable
|
+-+[1]TIME - [2]SIGNAL
+-+[1]TIME - [2]SIGNAL
+- ...
|
+-[2]othertable
|
+-+[1]TIME - [2]SIGNAL
+-+[1]TIME - [2]SIGNAL
+- ...
我该怎么做呢

-----------------编辑-----------------

我没有很好地解释自己,我需要的是:

给定一个名为“table”的表,我需要能够使用“字符串”作为该表中的“键”。这将是:

-- Name of my "container" table
Table = {}
引入的价值观

Time = '1 seconds' -- this value can change as desired
Value = 'logic' -- this value can change as desired
使用字符串键“RandomName”添加到我的主表“table”

每次我调用函数“AddNewValues()”,它都应该将“time”和“Value”中的值添加为该“RandomName”的“新条目”

因此,表格结果可能如下所示:

+table -- that contains
+-RandomName-- string key to access
+--"Time,Value"
+--"Time,Value"
+--"Time,Value"
+...
Table = { ["[1]othertable"] = {}, ["[2]othertable"] = {} }
table.insert(Table["[1]othertable"],  {TIME, SIGNAL})
然后,为了能够访问该表中的值,使用“RandomName”作为键:

function Load()
  for key, v in pairs(Table) do
    a = Table[key][RandomName][1] -- reference to "Time"
    b = Table[key][RandomName][2] -- reference to "Value"
    print('Time: ' .. a ..'/' .. 'Value: ' .. b)
  end
end

你只是开始不够深入。如果希望键下表的序列值等于RandomName的值,只需执行以下操作:

function Load()
  for _, v in ipairs(Table[RandomName]) do
    a = v[1] -- reference to "Time"
    b = v[RandomName][2] -- reference to "Value"
    print('Time: ' .. a ..'/' .. 'Value: ' .. b)
  end
end

对原问题的答复:

看起来您想要这样的东西:

+table -- that contains
+-RandomName-- string key to access
+--"Time,Value"
+--"Time,Value"
+--"Time,Value"
+...
Table = { ["[1]othertable"] = {}, ["[2]othertable"] = {} }
table.insert(Table["[1]othertable"],  {TIME, SIGNAL})
键为“[1]其他表”和“[2]其他表”

您可能更喜欢使用“othertable1”和“othertable2”等键。如果使用有效标识符,则可以删除某些语法:

Table = { othertable1 = {}, othertable2 = {} }
table.insert(Table.othertable1,  {TIME, SIGNAL})
事实上,您可能更喜欢在时间和信号方面做类似的事情。可以使用字符串键代替正整数索引:

Table = { othertable1 = {}, othertable2 = {} }
table.insert(Table.othertable1,  {TIME = 12.42, SIGNAL = 3.2})
print(Table.othertable1[1].TIME)