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

campare to metaTable

Started by Termanater13, 17 March 2014 - 04:20 AM
Termanater13 #1
Posted 17 March 2014 - 05:20 AM
Im looking for a way to compare a value to all of the contents of a table and return true if the Item is in the table. I know the simple solution to it is simply the following:
a = {"this","is","just","a","test"}
function tableCompare(with)
  for key, value in pairs(a) do
    if value == with then
	  return true -- if it reaches this its in table a
    end
  end
  return false -- If it reaches this it is not in table a
end

if tableCompare("test") then
  -- if true code
else
  -- if false code
end
but when done I want it to be as simple as to compare:
if a == "test" then
  -- if true code
else
  -- if false code
end

the issue I am having is the reference manual on lua meta tables (link) stats in the are that talks about the "eq" ("==" operation) that it also looks to see if they are the same type, and I am lost when they talk about "getcomphandler", and making sure they are the same type.
CometWolf #2
Posted 17 March 2014 - 05:24 AM
The function getcomphandler is defined right there. As you can see, all it does is check that the 2 elements are of the same datatype(table) then it compares their __eq metamethod. If they are the same, the metamethod is invoked. Otherwise, they are considered unequal.
Termanater13 #3
Posted 17 March 2014 - 05:28 AM
The function getcomphandler is defined right there. As you can see, all it does is check that the 2 elements are of the same datatype(table) then it compares their __eq metamethod. If they are the same, the metamethod is invoked. Otherwise, they are considered unequal.
Is this something I can overwrite so I can ignore that? If so can it be done when setmetatable is used?
CometWolf #4
Posted 17 March 2014 - 05:32 AM
I am unsure of wether the eq event listed there is the default __eq metamethod, or if it's the default == operation. Im guessing the former however, so i'd say just try it out. I've never used the eq meta
Lyqyd #5
Posted 17 March 2014 - 07:11 AM
No, you can't change how comparisons are handled at that low of a level. You'll either have to use a lookup table or just declare a simple function in your script and call it as necessary. Neither option is terribly difficult, and are certainly better solutions than trying to modify how equality comparisons are made. It would be easy to create odd edge and corner cases that way.