insert.table unique

Posted by Mahony on Wed 24 Sep 2014 07:55 PM — 6 posts, 28,958 views.

#0
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
Australia Forum Administrator #1
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
#2
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...? :)
Australia Forum Administrator #3
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?
USA Global Moderator #4
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
Amended on Thu 25 Sep 2014 01:34 PM by Fiendish
Australia Forum Administrator #5
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