Check if an element already exists in a table

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Check if an element already exists in a table

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Check if an element already exists in a table

Post 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.
When I write def I mean function.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Check if an element already exists in a table

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Check if an element already exists in a table

Post 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.
When I write def I mean function.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Check if an element already exists in a table

Post 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.
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Check if an element already exists in a table

Post 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
"I don't know."
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: Check if an element already exists in a table

Post 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:

Code: Select all

print(game[{"untitled"}])
But it printed:

Code: Select all

nil
Am I wrong at creating and calling a table with a table?
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Check if an element already exists in a table

Post by zorg »

The problem is that they are separately defined tables.

Code: Select all

{"untitled"} ~= {"untitled"}
It will return true if you do:

Code: Select all

game = {}
local a = {"untitled"}
game[a] = true
print(game[a])
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Check if an element already exists in a table

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests