无法读取多维数组(Table)
Can't read multi-dimensional array(Table)
Module = {}
tempData = {Visit = false, Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
Hats = {Basic = true, NightShade = false},
Robes = {Basic = true, NightShade = false},
Boots = {Basic = true, NightShade = false},
Swords = {Basic = true, NightShade = false}
}
function Module:ChangeLook(player, category, name)
tempData[player][category][name] = true
end
--[[ function Module:readStats()
for k, v in pairs(tempData['Stats']) do
print(k)
print(v)
wait()
end
end
function Module:readHats()
for k, v in pairs(tempData['Hats']) do
print(k)
print(v)
wait()
end
end
--These two works, but it's not effective if I have to make each function to read different array(table)]]
--[[ function Module:ReadAll()
for i = 1, #tempData do
for k = 1, #tempData do
print(tempData[i][k])
end
end
这个功能是我正在做的,但我不知道哪里出了问题?
我正在尝试读取整个数组(table)而不使用单独的函数来读取每个单独的数组。]]
return Module
你可以在你的情况下做这样的事情:
tempData = {
Visit = false,
Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
Hats = {Basic = true, NightShade = false},
Robes = {Basic = true, NightShade = false},
Boots = {Basic = true, NightShade = false},
Swords = {Basic = true, NightShade = false}
}
function ReadAll()
for k,v in pairs(tempData) do
if type(v) == 'table' then
for k2, v2 in pairs(v) do
print(k..":",k2,v2)
end
else
print(k..":",v)
end
end
end
ReadAll()
检查它是否是 table 并使用 key - value 对而不是 index - value.
Module = {}
tempData = {Visit = false, Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
Hats = {Basic = true, NightShade = false},
Robes = {Basic = true, NightShade = false},
Boots = {Basic = true, NightShade = false},
Swords = {Basic = true, NightShade = false}
}
function Module:ChangeLook(player, category, name)
tempData[player][category][name] = true
end
--[[ function Module:readStats()
for k, v in pairs(tempData['Stats']) do
print(k)
print(v)
wait()
end
end
function Module:readHats()
for k, v in pairs(tempData['Hats']) do
print(k)
print(v)
wait()
end
end
--These two works, but it's not effective if I have to make each function to read different array(table)]]
--[[ function Module:ReadAll()
for i = 1, #tempData do
for k = 1, #tempData do
print(tempData[i][k])
end
end
这个功能是我正在做的,但我不知道哪里出了问题? 我正在尝试读取整个数组(table)而不使用单独的函数来读取每个单独的数组。]]
return Module
你可以在你的情况下做这样的事情:
tempData = {
Visit = false,
Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
Hats = {Basic = true, NightShade = false},
Robes = {Basic = true, NightShade = false},
Boots = {Basic = true, NightShade = false},
Swords = {Basic = true, NightShade = false}
}
function ReadAll()
for k,v in pairs(tempData) do
if type(v) == 'table' then
for k2, v2 in pairs(v) do
print(k..":",k2,v2)
end
else
print(k..":",v)
end
end
end
ReadAll()
检查它是否是 table 并使用 key - value 对而不是 index - value.