我需要帮助知道如何使用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
好的,这允许我每次调用该函数时插入TIME和SIGNAL的值,因此该表将具有:
Table[1][1] = TIME
Table[1][2] = SINGAL
...
Table[...][1] = TIME
Table[...][2] = SIGNAL
但是......我需要将TIME和SIGNAL的值插入到表“Table”中的另一个表中,并且该表用作引用这些值的KEY ... TIME和SIGNAL ..
因此,结果表如下:
+Table
|
+-[1]othertable
|
+-+[1]TIME - [2]SIGNAL
+-+[1]TIME - [2]SIGNAL
+- ...
|
+-[2]othertable
|
+-+[1]TIME - [2]SIGNAL
+-+[1]TIME - [2]SIGNAL
+- ...
我该怎么办呢?
-----------------编辑-----------------
我没有很好地解释自己,我需要的是:
给定一个名为“Table”的表,我需要能够在该表中使用“strings”作为“键”。那将是:
-- Name of my "container" table
Table = {}
要引入的值
Time = '1 seconds' -- this value can change as desired
Value = 'logic' -- this value can change as desired
使用字符串键“RandomName”
添加到我的主表“表”-- "RandomName" can change as desired
function AddNewValues ()
table.insert (Table [RandomName], {Time, Value})
end
每当我调用函数“AddNewValues()”时,它应该将“Time”和“Value”中的值添加为“RandomName”的“NEW ENTRY”。
因此表格结果可能如下所示:
+table -- that contains
+-RandomName-- string key to access
+--"Time,Value"
+--"Time,Value"
+--"Time,Value"
+...
然后,为了能够访问该表内的值,使用“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
答案 0 :(得分:1)
你只是没有深入到表中。如果希望键下的表的序列值等于RandomName的值,只需执行:
require(ggplot2)
require(rgdax)
require(quantmod)
candles <- public_candles(product_id = "ETH-EUR")[c(1:250),] # Selected 250 to be able to see the candles
candles$candleLower <- pmin(candles$open, candles$close)
candles$candleMiddle <- NA
candles$candleUpper <- pmax(candles$open, candles$close)
# CandleMiddle has to be set to a value so candles can be plot.
# candles$candleMiddle <- NA raises: `Error: Discrete value supplied to continuous scale``
candles$candleMiddle <- 0.5*(candles$open+candles$close)
candles$fill <- ''
candles$fill[candles$open < candles$close] = 'green'
candles$fill[candles$fill ==''] = 'red'
candles$time <- as.POSIXct(candles$time)
#Add Moving Averages
candles$ma200 <- SMA(candles$close, 200)
candles$ma50 <- SMA(candles$close, 50)
#Graphing Step
g <- ggplot(candles, aes(x = time)) +
geom_boxplot(stat='identity', aes(group=time,
fill=fill,
lower=candleLower,
middle=candleMiddle,
upper=candleUpper,
ymin=low,
ymax=high))+
geom_line(aes(y=ma50, linetype='ma50'))+
geom_line(aes(y=ma200,linetype='ma200'))+
ylab("ETH-EUR")
g <- g + scale_x_datetime("", date_breaks = "15 min", date_labels = "%H:%M",
date_minor_breaks = "1 day")
g <- g + guides(linetype=guide_legend(title=''), fill=FALSE) # Remove 'fill' legend and just leave the one for the MA
g
回答原始问题:
看起来你想要这样的东西:
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
钥匙是&#34; [1] othe&amp;#34;和&#34; [2] othertable&#34;。
您可能更喜欢使用&#34; othertable1&#34;等关键字。和&#34; othertable2&#34;。如果您使用有效的标识符,则可以删除一些语法:
Table = { ["[1]othertable"] = {}, ["[2]othertable"] = {} }
table.insert(Table["[1]othertable"], {TIME, SIGNAL})
事实上,您可能更喜欢使用TIME和SIGNAL做类似的事情。您可以使用字符串键来代替正整数索引:
Table = { othertable1 = {}, othertable2 = {} }
table.insert(Table.othertable1, {TIME, SIGNAL})