gpt4 book ai didi

lua - 如何检查嵌套在lua表中的 bool 值

转载 作者:行者123 更新时间:2023-12-04 08:27:56 24 4
gpt4 key购买 nike

我是 lua 的新手,我在处理表中条目的基本按 bool 条件排序时遇到了麻烦。

`local tblFormReturn = {
{
['Name'] = 'Spike',
['Year'] = '10',
['House'] = 'Holmes',
['Form Returned'] = true
},
{
['Name'] = 'Elvis',
['Year'] = '11',
['House'] = 'Shaw',
['Form Returned'] = true
},
{
['Name'] = 'Michael',
['Year'] = '10',
['House'] = 'Langley',
['Form Returned'] = false
},
{
['Name'] = 'Chang',
['Year'] = '11',
['House'] = 'Holmes',
['Form Returned'] = false
}
}`
基本上,我希望能够拿到这张 table ,对于每一块,检查 child 是否在福尔摩斯家 (1) 以及他们是否已经返回了他们的表格 (2)。我的感觉是我需要根据 lua 手册成对运行一个 for 循环,但我对如何访问这些值感到困惑,因为每个块都是一个子表。我的尝试都是基于这样的事情。
for i,'Form Returned' in tblFormReturned('Form Returned') do
if 'Form Returned' == true then
if 'House' == 'Holmes' then
print ('Number of Holmes forms returned' +1)
end
end
end
我不知道如何使这项工作。非常感谢任何帮助。

最佳答案

这里有一些注意事项。
当您引用某些内容(通过使用单引号表示)时,您实际上将其变成了一个字符串。
for 循环使用 ipairs 遍历表(索引对,例如您的)或 pairs (用于字典表)。字典表被认为具有定义的键而不是索引键(例如 tblPets = {dog = "Fido", cat = "Sassy", duck = "Quackers} - 这将允许您返回 tblPets.dog (或 tblPets["dog"]) 以获取值)。
您的 print添加数字的语句不起作用。您不能向字符串添加数字。相反,您需要将计数设置为变量并添加到其中,前提是它是一个数字。
最后,您还可以结合 if语句合二为一,以便更容易。

formCount = 0 -- This initializes the variable formCount as an interger, starting with 0.
for i,v in ipairs(tblFormReturned) do -- This iterates through the table
if v["Form Returned"] and v.House == "Holmes" then -- Looks to see if the form returned is true and house is Holmes. Note that with boolean values, you do not have to see if it equals true or false. if v["Form Returned"] == true and this format returns the same answer.
formCount = formCount + 1 -- Adds 1 to the formCount
end -- end if statement
end -- end for loop
希望这对理解有所帮助。如果您有任何疑问,请不要犹豫,要求澄清。

关于lua - 如何检查嵌套在lua表中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65173976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com