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 ➜ insert.table unique

insert.table unique

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


Posted by Mahony   (27 posts)  Bio
Date Wed 24 Sep 2014 07:55 PM (UTC)
Message
Hi
Is there a function that insert only unique values or do I need to check for it myself? Coming form Zmud - the additem function inserted only unique values... If I need to check it I would appreciate the code if anyone have one. :P
Thank you
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 24 Sep 2014 09:15 PM (UTC)
Message
Are you referring to Lua tables?

If you insert with your own key, you will not get duplicates (of that key). However you need to check in advance if the item already exists.

Example:


require "tprint"

foo = { }  -- make a table

foo.nick = "hello"
foo.bar = "world"

tprint (foo)
print ""

foo.nick = "goodbye"

tprint (foo)


Output:


"bar"="world"
"nick"="hello"

"bar"="world"
"nick"="goodbye"


As you can see the key "nick" has changed from "hello" to "goodbye" in this case.




If you are wanting to use an existing value if it is there (eg. a counter of mobs killed, or experience gained) then you need to check if a particular key is there in advance, and if not, make that key. In the example below I add one every time a player kills a mob. The first time a particular player kills a mob we have to create that table entry, with a value of zero for the counter. Then it is possible to add one to it.


function countMobsKilled (who)

  -- make table if it doesn't exist
  if not mobsTable then
    mobsTable = { }
  end -- if

  -- make an entry for this person if necessary
  if not mobsTable [who] then
    mobsTable [who] = 0
  end -- if

  mobsTable [who] = mobsTable [who] + 1
    
end -- countMobsKilled 

-- test

countMobsKilled ("Mahony")
countMobsKilled ("Nick")
countMobsKilled ("Mahony")
countMobsKilled ("Mahony")

require "tprint"
tprint (mobsTable)


Output:


"Mahony"=3
"Nick"=1


For more details about Lua tables see:

http://www.gammon.com.au/forum/?id=4903

and:

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

- Nick Gammon

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

Posted by Mahony   (27 posts)  Bio
Date Reply #2 on Thu 25 Sep 2014 08:02 AM (UTC)
Message
I'm filling the table with values (keywords for equ like "amulet aardwolf"). The index/key is an increading number. I don't care the index. If I try to add "amulet aardwolf" the second time I don't want it to be added.

Here is my trigger from Zmud

#TRIGGER {| Keywords   : (*)} {#additem @dbname '%trimright(%remove("  |",%1))'}


So if the trigger is enabled and I identify stuff it is added and the additem funcions guarantee that there will be only unique values added.

The only way I see now with Mush is that I must go through the table with a cycle and check if the value is there...
Or...? :)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 25 Sep 2014 10:34 AM (UTC)
Message
Quote:

If I try to add "amulet aardwolf" the second time I don't want it to be added.


So, make "amulet aardwolf" the key. What is the problem with that?

- Nick Gammon

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

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #4 on Thu 25 Sep 2014 01:05 PM (UTC)

Amended on Thu 25 Sep 2014 01:34 PM (UTC) by Fiendish

Message
What Nick is saying is that you want a "bookkeeping" table rather than a "bag of data" table. (My own terminology that I invented just now for you. Don't bother looking it up.)

A bag of data is of the form:

data = {}
table.insert(data, "hello")
table.insert(data, "ocelot")
table.insert(data, "potato masher")


A bookkeeping table is of the form:

data = {}
data["hello"] = "data about hello"
data["ocelot"] = "data about ocelot"
data["potato masher"] = "data about potato masher"


In the bookkeeping table, you will only ever have one instance of "data about potato masher" because you can only have one "potato masher" key in your table.
If you try to assign "data about a different potato masher" to data["potato masher"] it will overwrite the first one and you will still only have one.
If you want to prevent overwriting and always keep the first one you add, you can instead do all your assignments as

data["potato masher"] = data["potato masher"] or "data about potato masher"

This works because data["potato masher"] on the right side of the equal sign is nil if it has not yet been assigned and the value if it has, and because of how Lua handles nil and strings in boolean logic.
The more explicit form of this is:

if data["potato masher"] == nil then
   data["potato masher"] = "data about potato masher"
end

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 25 Sep 2014 10:12 PM (UTC)

Amended on Fri 26 Sep 2014 08:06 AM (UTC) by Nick Gammon

Message
And if you want to store more than one thing about "amulet aardwolf" you make the value a table.

http://www.gammon.com.au/forum/?id=4903 <-- Tables in Lua
http://www.gammon.com.au/forum/?id=6036 <-- Lua tables in detail

For example:


data = {}  -- Make the table. Only do this one.

data ["amulet aardwolf"] = 
  {
   value = 42,
   weight = 15,
   type = "jewel",
   quantity = 1,
  }  -- end of table


Once you have got this far you can update the information. eg


data ["amulet aardwolf"].quantity = 2 


Tables can be serialized (saved) for next time:

http://www.gammon.com.au/forum/?id=4960

- 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.


21,295 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.