checking if a value is not on table ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

checking if a value is not on table ?

Post by Vrx8 » Mon Sep 11, 2017 11:21 am

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 ?

Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: checking if a value is not on table ?

Post by Vrx8 » Mon Sep 11, 2017 11:26 am

Oh yeah.. I also don't want to use any extra table to get the answer

User avatar
raidho36
Party member
Posts: 1676
Joined: Mon Jun 17, 2013 12:00 pm

Re: checking if a value is not on table ?

Post by raidho36 » Mon Sep 11, 2017 11:43 am

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.

grump
Party member
Posts: 363
Joined: Sat Jul 22, 2017 7:43 pm

Re: checking if a value is not on table ?

Post by grump » Mon Sep 11, 2017 1:38 pm

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

User avatar
ivan
Party member
Posts: 1301
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: checking if a value is not on table ?

Post by ivan » Mon Sep 11, 2017 3:31 pm

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)

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: checking if a value is not on table ?

Post by davisdude » Tue Sep 12, 2017 1:51 am

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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

grump
Party member
Posts: 363
Joined: Sat Jul 22, 2017 7:43 pm

Re: checking if a value is not on table ?

Post by grump » Tue Sep 12, 2017 4:05 am

davisdude, OP wants to check the table for a value, not for some random key.

User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: checking if a value is not on table ?

Post by erasio » Tue Sep 12, 2017 8:46 am

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

Code: Select all

for k, v in paits(t) do 
Edit: Overlooked a reply. Grump is right. I thought he was responding to Ivan.

My bad!
Last edited by erasio on Tue Sep 12, 2017 8:56 am, edited 1 time in total.

grump
Party member
Posts: 363
Joined: Sat Jul 22, 2017 7:43 pm

Re: checking if a value is not on table ?

Post by grump » Tue Sep 12, 2017 8:50 am

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.

User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: checking if a value is not on table ?

Post by erasio » Tue Sep 12, 2017 8:55 am

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^^

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests