我有下面的lua代码,可以打印出设备的Mac地址。
local sc = Command.Scan.create() 
local devices = sc:scan() 
local topicMac
local list = {}
for _,device in pairs(devices) do 
   print(device:getMACAddress())
   list[device] = device:getMACAddress()
end
  
topicMac = list[0]
print(topicMac)因为有几个地址,并且它们列在一个表中,所以我只想将第一个地址保存到本地变量"topicMac“中。我尝试通过在数组中添加第一个索引(0或1)来达到第一个值。
为什么我会得到nil作为返回值?
发布于 2021-02-11 03:45:52
next关键字可用作变量函数,以便从表中检索表中的第一个索引和值
local index, value = next(tab) -- returns the first index and value of a table所以在你的例子中:
local _, topicMac = next(list)https://stackoverflow.com/questions/66143375
复制相似问题