连接所有值并将它们附加到同一个变量

时间:2013-01-17 16:35:12

标签: csv lua append concatenation

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

  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

1 个答案:

答案 0 :(得分:2)

  

我不断收到错误:“尝试连接全局'arglist'(零值)”

因为您尝试在代码的第31行连接arglist(零值):

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, ',')