So I am trying to capture a list of items in a store and then store it myself in a table. I got the capture going, and it now captures 'name' for the item name, 'type' for the item type and 'amount' which is a number of the amount, i.e. name = 'bag', type = 'clothing' and amount = '3'. I want to add that to an array so I can have this capture all the items in the store into that array, how would I do that? Also, how can I have it later do something like 'list bags' and it will list everything that matched sword? THank you.
1 Answer
"Array" is a foreign concept but you can do a lot with a table.
Here is a table with names as the key, which you can use if the names are unique.
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
Output:
bag1 sword 3
I suggest that use a number of the amount so you have numeric operations available for it.