This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sci4me's profile picture

First parameter (table) of __index and comparing tables?

Started by sci4me, 06 December 2014 - 04:17 AM
sci4me #1
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…)
KingofGamesYami #2
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 on 06 December 2014 - 04:31 AM
sci4me #3
Posted 06 December 2014 - 05:48 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

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 on 06 December 2014 - 05:12 AM
KingofGamesYami #4
Posted 08 December 2014 - 07:46 PM
-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