|
Is there a way to find a difference in tables and identify which index/value it is?
For example:
table1 = {"cat", "dog"}
table2 = {"cat", "dog", "catdog"}
if table1 ~= table2 then
blah blah
end
Any replies are appreciated :D. |
|
|
You can do something like:
local function compareArrays(tbl1, tb2)
if #tbl1 ~= #tbl2 then return false; end
for key = 1, #tbl1 do
if tbl1[key] ~= tbl2[key] then return false; end
end
return true;
end |
|
|
Thanks for the reply!
What is the purpose of putting local behind function? I understand scopes of variables, but I don't really understand the reasoning behind local functions.
Also what's up with the semi-colons? |
|
|
it is the same thing like local variables I assume |
|
|
'What is the purpose of putting local behind function? I understand scopes of variables, but I don't really understand the reasoning behind local functions.'
In Lua, references to functions are held by variables.
So: function a() end
Is the same as: a = function() end;
Whereas: local function a()
Is the same as: local a; a = function() end;
Also I use semi-colons for no reason, you don't need them. |
|
|
Alright, but what is the difference that local makes? I mean, I can see that a variable has a different value based on where you put inside of a function, if it's local. |
|
|
It can only be accessed within that scope, so for example:
do -- create a new scope
local function abc()
print("hi");
end
abc(); abc(); -- print hi twice
end
abc(); -- error, abc is nil ehre |
|
|
You put semicolons because you used java, right? I know it's not required for LUA, but that's why you use them, right? |
|
|
What, what is LUA again?
-The [Guy] |
|
|
Not because of Java, also woops I read what you wanted wrong.
Mine gives you whether there were any differences at all or not, not all the differences.
All you really need to do though is create a table, for every difference insert the key (remove the return in the loop) and return that table. |
|