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.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.
Check if an element already exists in a table
Re: Check if an element already exists in a table
- 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
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.Azhukar wrote:Wrong.
Let's call "container" the table in which other tables are being inserted as keys, and "item" the table being inserted as key.S0lll0s wrote:It does lead to problems when the table moves out of scope in the meantime though
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.
Re: Check if an element already exists in a table
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.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.
- 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
Well, none of those.Azhukar wrote:Is it alright if I say "Incorrect." or "Negative."?
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.
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.Azhukar wrote: I wouldn't want to insult anybodies fragile psyches with such mean words.
When I write def I mean function.
Re: Check if an element already exists in a table
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.
- BruceTheGoose
- Citizen
- Posts: 76
- Joined: Sat Sep 20, 2014 2:54 pm
Re: Check if an element already exists in a table
That works perfect. Thank you!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
If anyone is curious, this code was for this:
https://www.youtube.com/watch?v=eb17mMxM4C8
"I don't know."
Re: Check if an element already exists in a table
I accept that. I tested the "index a table with a table" with:Azhukar wrote:Wrong.Ortimh wrote:you can't index a table with a table.
Code: Select all
game = {}
game[{"untitled"}] = true
Code: Select all
print(game[{"untitled"}])
Code: Select all
nil
- 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
The problem is that they are separately defined tables.
It will return true if you do:
Code: Select all
{"untitled"} ~= {"untitled"}
Code: Select all
game = {}
local a = {"untitled"}
game[a] = true
print(game[a])
Me and my stuff True 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.
- 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
yeah or get it with a for loop
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
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
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests