# Comparing Tables

Hi, I’ve Created A Function That I Think Is Useful

``````                               **CODE**
function CompareTable(Value,Table)
if typeof(Table) ~= "table" then
error("table expected, got " .. typeof(Table))
end
for i,v in pairs(Table) do
if v == Value then
return true
end
end
return false
end
``````

Easy But Efficient And Useful

## Usecases

• Do not use or to for not lengthen your code

Module

I think that it would be better if instead of returning true you return i.

is a boolean function

I can give you the code

``````function CompareTabletoIndex(Value,Table)
if typeof(Table) ~= "table" then
error("table expected, got " .. typeof(Table))
end
for i,v in pairs(Table) do
if v == Value then
return i
end
end
return false
end``````
``````function compare(v, t)
assert(type(t) == 'table', 'no')
for _, _v in next, t do
if _v == v then
return true
end
end
return false
end
``````
1 Like

I think it would be good to give a type to the Table parameter, and enable strict typing, that way you don’t have to perform a check to make sure the value passed is a table type.

I don’t know the assert ​function

basically an error if statement in one line

assert(statement, this code will run if the statement is false)

``````if typeof(Table) ~= "table" then
error("table expected, got " .. typeof(Table))
end
``````

does the same thing as

``assert(typeof(Table) == "table", "table expected, got " .. typeof(Table))``

is it like this disassembled?

function assert(statement,error)

if not statement then

error(error)

end

end)

This linear search algorithm is equivalent to `table.find` which is defined in C++ and thus will run faster than any Lua implementation.