Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Adding Store Items to a table

Adding Store Items to a table

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Rene   (46 posts)  Bio
Date Sun 08 Oct 2017 12:57 PM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 10 Oct 2017 08:02 PM (UTC)
Message
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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,236 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.