Adding Store Items to a table

Posted by Rene on Sun 08 Oct 2017 12:57 PM — 2 posts, 14,666 views.

#0
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 = 'sword', type = 'weapon' 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 list 'list sword' and it will list everything that matched sword?
THank you.
Australia Forum Administrator #1
See my post about Lua tables: http://www.gammon.com.au/forum/?id=4903

And also: http://www.gammon.com.au/forum/?id=6036

Basically you would make a table of tables.

For example:


itemsTable = { }  -- make table of all items in store

-- for each item:

newItem = { }  -- empty table

newItem.name = 'sword'
newItem.type = 'weapon'
newItem.amount = 3


table.insert (itemsTable, newItem)

newItem = { }  -- empty table

newItem.name = 'knife'
newItem.type = 'weapon'
newItem.amount = 4

table.insert (itemsTable, newItem)


require "tprint"

tprint (itemsTable)  -- view table

-- look through table:

for k, v in ipairs (itemsTable) do

  print ("name =", v.name)
  print ("type =", v.type)
  print ("amount =", v.amount)

end -- for each item