-1

基本的に、データをプルする必要があるデバイスがいくつかあります。温度測定値が設定された制限を下回ったり上回ったりすると、複数の電子メールが届きます。for制限内または制限を超えている現在のすべてのデバイスの状態を 1 つのメールに含めるループが必要です。

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ます。入力でテーブル デバイス パラメータを受け取る関数の形式で、各要素がサブテーブルであるコードの拡張バージョンを次に示します。

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 に答える