-1

基本上,我有几个设备需要从中提取数据。当温度测量值低于或超过设定限制时,我会收到多封电子邮件。我希望有一个for循环将所有当前低于或超过限制的设备状态包含在一封电子邮件中。

body = for device_name, 1 do 
  device_name++ -- I get error here because unexpected symbol near 'for'
  "Device Name: " ..device_name.. "\nDevice Location: " ..device_location.. 
  "\n--------------------------------------------------------------" .. 
  "\nCurrent Temperature: " ..temperature.." F".. "\nTemperature Limit: ("..
  t_under_limit.. "-" ..t_over_limit.. " F)" .."\n\nCurrent Humidity Level: " .. 
  humidity .. "%".. "\nHumidity Limit: (" .. h_under_limit.. "-" ..h_over_limit.. "%)" 
  .. "\n\n-------Time Recorded at: " ..os.date().. "-------"})
end, -- end for
4

2 回答 2

2

lua 中没有 variable++ 语法。你需要做

variable = variable + 1

此外,您不能将一些 for 循环构造分配给变量。所以这个声明

body = for device_name, 1, ...

无效。也许你的意思是……

local body = ""
for device_name = 1, 1
    device_name = device_name + 1
    body = body.. "my custom message stuff here"
end
于 2012-10-18T17:44:03.010 回答
1

如前所述,++Lua 中没有运算符。此外,for循环的语法与您编写的不同。

我想补充一点,之后使用string.format. 这是您的代码的增强版本,形式为 aa 函数,在输入中采用表设备参数,每个元素都是一个子表:

local report_model = [[
Device Name: %s
Device Location: %s
--------------------------------------------------------------
Current Temperature: %d °F
Temperature Limit: (%d-%d °F)
Current Humidity Level: %d %%
Humidity Limit: (%d-%d %%)

-------Time Recorded at: %s-------]]

function temp_report(devices)
  local report = {}
  for i=1,#devices do 
    local d = devices[i]
    report[i] = report_model:format(d.name, d.location,
      d.temperature, d.t_under_limit, d.t_over_limit,
      d.humidity, d.h_under_limit, d.h_over_limit,
      os.date())
   end
   return table.concat(report)
end
于 2012-10-18T19:38:16.273 回答