• See if two references in a table are the exact same?
    6 replies, posted
Not sure what the correct term for this is, but I want to see if two values/keys in a table are the exact same. So let's say we have a table t, with t[1] = 3, and t[2] = 3 as well. If I compare these two, it will come out as true. Same thing with keys. Is there a way to compare references instead of values? Thanks.
Can you be more descriptive? If you're talking about pointers, then there's no way for numbers.
You could probably do [code] local t = {3, 3} if t[1] == t[2] then return true end [/code] and if you want to store true in a variable: [code] local t = {3, 3} local ass if t[1] == t[2] then ass = true end [/code]
[QUOTE=code_gs;50346797]Can you be more descriptive? If you're talking about pointers, then there's no way for numbers.[/QUOTE] Not sure how else I can explain it. I want to see if two members of a table are the exact same. So I want to do something like t[1] == t[2] and I want it to return false even if the variables held by them are the same. I want to compare the members ([1] and [2] in this case) rather than the variables held by them.
[code]if ( 1 == 2 ) then[/code] ?
A table can't have two of the same keys. If you mean between two tables, you can just check if a value exists in that key in both tables. [lua]if t1[key] ~= nil and t2[key] ~= nil then[/lua] If you want to search two tables and find all keys they both have, you can do a loop through one and check if the other also has a value in that key. [lua]function FindCommonKeys(t1, t2) local found = {} for k,v in pairs(t1) do if t2[k] ~= nil then table.insert(found, k) end end return found end[/lua]
[QUOTE=overki11;50346836]Not sure how else I can explain it. I want to see if two members of a table are the exact same. So I want to do something like t[1] == t[2] and I want it to return false even if the variables held by them are the same. I want to compare the members ([1] and [2] in this case) rather than the variables held by them.[/QUOTE] Strings, numbers, and booleans are compared by their values, not references. Functions, tables, etc are by reference. No two keys in a table can be equal.
Sorry, you need to Log In to post a reply to this thread.