|
| 03 Oct 2015 09:46 PM |
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. |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 09:48 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 09:53 PM |
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? |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 09:55 PM |
| it is the same thing like local variables I assume |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:04 PM |
'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. |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:24 PM |
| 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. |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:26 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:34 PM |
| You put semicolons because you used java, right? I know it's not required for LUA, but that's why you use them, right? |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:35 PM |
What, what is LUA again?
-The [Guy] |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2015 10:35 PM |
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. |
|
|
| Report Abuse |
|
|