Page 2 of 2
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 2:35 pm
by Azhukar
S0lll0s wrote:I had this issue in a nother project, I had two different table "ids" (the memory address) but the same output on serialization.
table.join is not proper table serialization. There can be 2 different tables with identical keys, values and metadata, making even exact serialization produce identical results.
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 2:41 pm
by kikito
Azhukar wrote:Wrong.
It is a bit rude to quote someone and then start answering with "Wrong." (even when you are convinced it is wrong). Please try to express your disagreement a bit less ... intensely in the future.
S0lll0s wrote:It does lead to problems when the table moves out of scope in the meantime though
Let's call "container" the table in which other tables are being inserted as keys, and "item" the table being inserted as key.
If you are ok with container being an "owner" of item (meaning that it item will not be garbage-collected until it is removed from container with container[item] = nil), then you don't have to do anything; as long as it is a key in container, item will not be deleted. This is the usual case; it works well for things like lists of enemies, which is the typical case for using a list of ids too - as a result, in Lua you very rarely need lists of ids.
If you *don't want* your container to be the owner of item (so other people are in charge of deciding when to delete item, but not container) then you must declare container as a weak table (which is not difficult). You will also have to check with the "real owners" of item when you are doing a for loop over container, and ask them "is this item a real item, or has it been erased?" before using it.
But that kind of usage is not usual at all (it's for things like caches of items), and the id-based solution does not cover the usecase well anyway.
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 2:51 pm
by Azhukar
kikito wrote:It is a bit rude to quote someone and then start answering with "Wrong." (even when you are convinced it is wrong). Please try to express your disagreement a bit less ... intensely in the future.
I didn't realize telling someone he is wrong is rude or intense. Is it alright if I say "Incorrect." or "Negative."? I wouldn't want to insult anybodies fragile psyches with such mean words.
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 4:40 pm
by kikito
Azhukar wrote:Is it alright if I say "Incorrect." or "Negative."?
Well, none of those.
I am no native English speaker and no literary expert, but here's a rule of thumb: If you have an argument to make, just make it. Use words to build and improve ideas inside other's minds, not as torpedoes to destroy the ones they had.
Azhukar wrote: I wouldn't want to insult anybodies fragile psyches with such mean words.
I'm not trying to protect any "fragile psyche" from insult. I'm trying to help someone whose statements are being devaluated by his tone.
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 4:46 pm
by Azhukar
I appreciate the input but I try to be as succinct as possible, when there is more than 1 other possibility to the one I call wrong, I explain the reasoning why it is wrong. It is akin to talking away from keyboard, you start with "You are wrong because...". I guess I could say "Opposite is true.", but it's really just word fluff.
Re: Check if an element already exists in a table
Posted: Mon Nov 10, 2014 11:18 pm
by BruceTheGoose
Azhukar wrote:table.insert(self.targets,v) inserts value v at the beginning of your table.
"self.targets[v] == nil" checks whether any value exists at key v.
You mixed up values and keys.
I'm guessing you want your value to exist only once in your table. In that case you can simply use the value itself as a key for the table its in.
So your code would look something like this:
Code: Select all
if self.targets[v] == nil then
self.targets[v] = true
else
print("already exists!")
end
That works perfect. Thank you!
If anyone is curious, this code was for this:
https://www.youtube.com/watch?v=eb17mMxM4C8
Re: Check if an element already exists in a table
Posted: Tue Nov 11, 2014 7:10 am
by Ortimh
Azhukar wrote:Ortimh wrote:you can't index a table with a table.
Wrong.
I accept that. I tested the "index a table with a table" with:
Code: Select all
game = {}
game[{"untitled"}] = true
Well I tried to call
game[{"untitled"}] with
print:
But it printed:
Am I wrong at creating and calling a table with a table?
Re: Check if an element already exists in a table
Posted: Tue Nov 11, 2014 7:21 am
by zorg
The problem is that they are separately defined tables.
It will return true if you do:
Code: Select all
game = {}
local a = {"untitled"}
game[a] = true
print(game[a])
Re: Check if an element already exists in a table
Posted: Thu Nov 13, 2014 3:22 am
by Positive07
yeah or get it with a for loop
Code: Select all
keys = {}
tab = {{"HELLO", "WORLD"} = {"THIS", "IS", "AWESOME"}}
for k,v in pairs(tab) do
if type(k) == "table" then
keys[#keys + 1] = k
print ("Key contains the following elements:")
for u,w in pairs (k) do
print(" "..u.." - "..w)
end
end
if type (v) == "table" then
print ("Value contains the following elements:")
for u,w in pairs(v) do
print(" "..u.." - "..w)
end
end
end
print(tab[keys[1]][3]) --AWESOME
Minimized version:
Code: Select all
keys = {}
tab = {{"HELLO", "WORLD"} = {"THIS", "IS", "AWESOME"}}
for k,v in pairs(tab) do
if type(k) == "table" then
keys[#keys + 1] = k
end
end
print(tab[keys[1]][3]) --AWESOME