【问题标题】:Lua print a nested tableLua 打印嵌套表
【发布时间】:2019-03-10 11:00:38
【问题描述】:

我有一张“天气”表。我插入特定日期的天气状况。我似乎无法编写打印“天气”内容的函数(请参阅下文了解我尝试过的内容。

day = "Friday"
conditions = {"Sunny", "85", "windy"}
weather = {{}}    --nested table


for k, v in pairs(conditions) do     
    weather[day] = {[k]=v}  
end

我尝试了两种方法来打印天气表,但都不起作用。

for k, v in pairs(weather) do 
print(k, v)
end

---- Output ---
1       table: 0x2542ae0
Friday  table: 0x25431a0

这也不起作用,但我认为它会

for k, v in pairs(weather) do 
    for l, w in pairs(v) do
    print(l, w)
    end
end

----Output----
3   windy

【问题讨论】:

    标签: multidimensional-array lua nested lua-table


    【解决方案1】:

    您将在第一个循环中覆盖weather[day],因此只保留最后一个值。

    我想你只想要这个,而不是那个循环:

    weather[day] = conditions  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2015-08-11
      相关资源
      最近更新 更多