Jump to content




First parameter (table) of __index and comparing tables?


  • You cannot reply to this topic
3 replies to this topic

#1 sci4me

  • Members
  • 225 posts
  • LocationEarth

Posted 06 December 2014 - 05:17 AM

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...)

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 December 2014 - 05:27 AM

First off: __index doesn't take a parameter. I believe you want this.

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 sci4me

  • Members
  • 225 posts
  • LocationEarth

Posted 06 December 2014 - 05:48 AM

View PostKingofGamesYami, on 06 December 2014 - 05:27 AM, said:

First off: __index doesn't take a parameter. I believe you want this.

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... I don't think it matters what the elements in the table are, as long as the tables are the same table. <--- that makes a "lot" of sense...

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.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 08 December 2014 - 07:46 PM

View Postsci4me, on 06 December 2014 - 05:48 AM, said:

-snip-
Oh, I am talking about __index as a function by the way...

*facepalm* I forgot __index could be a function...

As for comparing tables, yeah
local t = {}
print( t == t ) --#prints true






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users