添加到数组并搜索它们

问题描述:

所以我试图捕获商店中的商品列表,然后将它自己存储在一张表中。 我得到了捕获,它现在捕获项目名称的'名称',项目类型的'类型'和数量的数量即'数量',即name ='bag',type ='clothing'和金额='3'。 我想将其添加到数组中,以便我可以将商店中的所有商品都捕获到该数组中,我该怎么做? 另外,我怎么能稍后做一些像'名单包',它会列出匹配剑的一切? 谢谢你。添加到数组并搜索它们

“数组”是一个外国概念,但您可以使用表格做很多事情。

这是一张表格,其中的名称是关键字,如果名称是唯一的,则可以使用该表格。

name = "bag1" 
type = "sword" 
amount = 3 


store = store or {} -- Lua idiom for initializing a variable to an empty table 
        -- if it doesn't already have a value 
store[name] = { type = type, amount = amount } 


for name, item in pairs(store) do 
    if item.type == "sword" then 
     print(name, item.type, item.amount) 
    end 
end 

输出:

bag1 sword 3 

我建议使用一些量,以便你们有它的数字业务。