Page 1 of 2
checking if a value is not on table ?
Posted: Mon Sep 11, 2017 11:21 am
by Vrx8
for example:
Code: Select all
love.load()
a = 9
table = {}
for i=0, 10, 1 do
table.insert(table, i)
end
end
love.draw()
for i,v in pairs(table) do
if v ~= a then
--print something
end
end
end
so what i want to do here is to print something out only once if the value is not on table, how do i do it ?
Re: checking if a value is not on table ?
Posted: Mon Sep 11, 2017 11:26 am
by Vrx8
Oh yeah.. I also don't want to use any extra table to get the answer
Re: checking if a value is not on table ?
Posted: Mon Sep 11, 2017 11:43 am
by raidho36
You will have to use one extra variable to keep track of table contents. Otherwise the function will be stateless and you won't be able to tell anything about array other than its current element.
Re: checking if a value is not on table ?
Posted: Mon Sep 11, 2017 1:38 pm
by grump
Code: Select all
local t = {}
for i = 0, 10 do table.insert(t, i) end
local find = 9
local found = false
for index, value in ipairs(t) do
if value == find then
found = true
break
end
end
if not found then
print("something")
end
Re: checking if a value is not on table ?
Posted: Mon Sep 11, 2017 3:31 pm
by ivan
If you want to know if a value is inside the table, you might as well return the index.
You should probably check if the search value is non-nil to prevent bugs:
Code: Select all
--- Finds the first occurrence in a list
-- @param t Table
-- @param s Search value
-- @param o Starting index (optional)
-- @return Numeric index or nil
function table.find(t, s, o)
o = o or 1
assert(s ~= nil, "second argument cannot be nil")
for i = o, #t do
if t[i] == s then
return i
end
end
end
Note that the function works only with numerically indexed tables with no gaps.
Returns the first index in case of duplicates, usage:
Code: Select all
local t = {}
for i=1, 10 do
t[i] = i - 1
end
local intable = (table.find(t, 0) ~= nil)
Re: checking if a value is not on table ?
Posted: Tue Sep 12, 2017 1:51 am
by davisdude
Maybe I'm missing something here, but it seems like the simplest (and most efficient) solution would be to just check if that element is nil before or after the loop
Code: Select all
local tab = { a = 1, b = 2, c = 3 }
local key = 'testKey'
local notInTable = tab[key] == nil
if notInTable then
print( 'The key "' .. key .. '" is not present in the table' )
end
-- Loop over table
Re: checking if a value is not on table ?
Posted: Tue Sep 12, 2017 4:05 am
by grump
davisdude, OP wants to check the table for a value, not for some random key.
Re: checking if a value is not on table ?
Posted: Tue Sep 12, 2017 8:46 am
by erasio
grump wrote: ↑Tue Sep 12, 2017 4:05 am
davisdude, OP wants to check the table for a
value, not for some random
key.
We are in lua. Any value will evaluate to true, nil to false.
If you return the first key or nothing at all, you can use it for condition checking as if you'd return a bool. Just multiple occurrences of the same value aren't covered.
Returning the value you provide as parameter isn't helpful. Having access to the index and a condition function at once is.
This should work for string index table as well if you use
Edit: Overlooked a reply. Grump is right. I thought he was responding to Ivan.
My bad!
Re: checking if a value is not on table ?
Posted: Tue Sep 12, 2017 8:50 am
by grump
What? OP wants to know if a specific value is not in a table. If you want to check for a specific value, you have to iterate over the table. The keys have absolutely no relevance here.
Re: checking if a value is not on table ?
Posted: Tue Sep 12, 2017 8:55 am
by erasio
I skipped a reply and thought you responded to Ivan that his response is bad.
I shouldn't do this on mobile... My bad!
Was just about to delete it but then you replied too quickly^^