0

我正在尝试连接所有值并将它们附加到同一个变量,然后写入字符串的内容。我不断收到错误消息:“尝试连接全局 'arglist'(一个 nil 值)”

  eo_device = {}

  eo_device[1] = {node = "ME", t_object = "AV1", h_object = "AV2" }
  eo_device[2] = {node = "ME", t_object = "AV3", h_object = "AV4" }
  eo_device[3] = {node = "ME", t_object = "AV5", h_object = "AV6" }
  eo_device[4] = {node = "ME", t_object = "AV7", h_object = "AV8" }
  eo_device[5] = {node = "ME", t_object = "AV9", h_object = "AV10" }
  eo_device[6] = {node = "ME", t_object = "AV11", h_object = "AV12" }
  eo_device[7] = {node = "ME", t_object = "AV13", h_object = "AV14" }
  eo_device[8] = {node = "ME", t_object = "AV15", h_object = "AV16" }
  eo_device[9] = {node = "ME", t_object = "AV17", h_object = "AV18" }
  eo_device[10] = {node = "ME", t_object = "AV19", h_object = "AV20" }
  eo_device[11] = {node = "ME", t_object = "AV21", h_object = "AV22" }
  eo_device[12] = {node = "ME", t_object = "AV23", h_object = "AV24" }
  eo_device[13] = {node = "ME", t_object = "AV25", h_object = "AV26" }
  eo_device[14] = {node = "ME", t_object = "AV27", h_object = "AV28" }
  eo_device[15] = {node = "ME", t_object = "AV29", h_object = "AV30" }


-- Generate string with all arguments to be written to CSV
for deviceindex = 1,#eo_device do

 device_name = scl.nodes[eo_device[deviceindex].node][eo_device[deviceindex].t_object .. "_Object_Name"].value -- point to Object Names
 description = scl.nodes[eo_device[deviceindex].node][eo_device[deviceindex].t_object .. "_Description"].value -- point to Description of all objects     
 temperature = scl.nodes[eo_device[deviceindex].node][eo_device[deviceindex].t_object .. "_Present_Value"].value -- value of temperature object
 humidity = scl.nodes[eo_device[deviceindex].node][eo_device[deviceindex].h_object .. "_Present_Value"].value -- value of humidity object


 buflist = device_name .. "," .. description .. "," .. temperature .. "," .. humidity 

 arglist = arglist .. "," .. buflist  

 end -- end for

--then once outside of the loop:
arglist = arglist .. "," .. buflist 

buffer = os.date() .. arglist .. "\r\n"

file, info = io.open("trend.csv", "a+") -- write to CSV
if file then
file:write(buffer)
file:close()
end -- end if
4

1 回答 1

2

我不断收到错误消息:“尝试连接全局 'arglist'(一个 nil 值)”

因为您试图arglist在代码的第 31 行连接(一个 nil 值):

arglist = arglist .. "," .. buflist  

arglist从未初始化,因此当您尝试在 的右侧进行连接时=,您会收到该错误。


关于这个循环的旁注:

for deviceindex = 1,#eo_device do

    description = scl.nodes[eo_device[deviceindex].node][eo_device[deviceindex].t_object .. "_Description"].value

Lua 有一个“通用”for 循环,它允许您遍历 的所有元素eo_device,为您提供索引值,因此您不必在循环内建立索引。这将清理您的代码:

for deviceindex, device in ipairs(eo_device) do

    description = scl.nodes[device.node][device.t_object .. "_Description"].value

关于此代码的旁注:

buflist = device_name .. "," .. description .. "," .. temperature .. "," .. humidity 

像这样构建字符串是非常低效的。在循环中执行此操作将导致数千个字符串连接并产生大量垃圾。更好的方法是将所有字符串部分添加到表中,然后通过table.concat.

例如,要创建一个包含eo_device表中所有字符串的逗号分隔字符串:

local buffer = {}

for i,device in ipairs(eo_device) do
    table.insert(buffer, device.node)
    table.insert(buffer, device.t_object)
    table.insert(buffer, device.h_object)
end

devices = table.concat(buffer, ',')
于 2013-01-17T17:14:23.233 回答