Hey guys. So, I need something explained. First of all, what exactly is the first parameter in __index and __newindex metamethods? Secondly, can you compare tables? (just making sure...)
First parameter (table) of __index and comparing tables?
Started by sci4me, Dec 06 2014 05:17 AM
3 replies to this topic
#1
Posted 06 December 2014 - 05:17 AM
#2
Posted 06 December 2014 - 05:27 AM
First off: __index doesn't take a parameter. I believe you want this.
Second: Sort of...
Second: Sort of...
print( {} == {} ) --#prints false
local compareTables
function compareTables( tA, tB )
for k, v in pairs( tA ) do
if type( v ) ~= "table" and v ~= tB[ k ] then
return false
elseif type( v ) == "table" and ( type( tB[ k ] ) ~= "table" or not compareTables( v, tB[k] ) ) then
return false
end
end
return true
end
Edited by KingofGamesYami, 06 December 2014 - 05:31 AM.
#3
Posted 06 December 2014 - 05:48 AM
KingofGamesYami, on 06 December 2014 - 05:27 AM, said:
First off: __index doesn't take a parameter. I believe you want this.
Second: Sort of...
Second: Sort of...
print( {} == {} ) --#prints false
local compareTables
function compareTables( tA, tB )
for k, v in pairs( tA ) do
if type( v ) ~= "table" and v ~= tB[ k ] then
return false
elseif type( v ) == "table" and ( type( tB[ k ] ) ~= "table" or not compareTables( v, tB[k] ) ) then
return false
end
end
return true
end
Uhm... __index takes a table and a key... http://www.lua.org/pil/13.4.4.html
As for comparing tables...
What I want:
local table = {}
print(compare(table, table)) -- true
Oh, I am talking about __index as a function by the way...
Edited by sci4me, 06 December 2014 - 06:12 AM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











