checking if a value is not on table ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: checking if a value is not on table ?

Post by grump »

Ah sorry dude :)
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: checking if a value is not on table ?

Post by sphyrth »

:rofl:
This mishap made me realize why people make it a habit to tag the name of the person they're responding to.
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: checking if a value is not on table ?

Post by Vrx8 »

ivan wrote: 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)
thx
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: checking if a value is not on table ?

Post by Vrx8 »

found out a better way to do it for me.. using raidho's method

Code: Select all

load()

tableq = {}
for i=0, 10, 1 do
table.insert(tableq, i)
end
a = 99
b = 0
end

update()

for i,v in pairs(tableq) do
if v ~= a then
b = b + 1
end
end

if b ~= #tableq then
b = 0
end
end

function love.draw()

if b == #tableq then
lg.print("a is not in table")
b = 0
end
end
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: checking if a value is not on table ?

Post by Vrx8 »

using raidho's method

Code: Select all

function love.load()
tableq = {}
for i=0, 10, 1 do
table.insert(tableq, i)
end
a = 9
b = 0
end

function love.update()
for i,v in pairs(tableq) do
if v ~= a then
b = b + 1
end
end

if b ~= #tableq then
b = 0
end
end

function love.draw()
if b == #tableq then
lg.print("a is not in table")
b = 0
else
lg.print("a is in table")
end
end
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: checking if a value is not on table ?

Post by ivan »

Depends on what you're trying to do Vrx8.

The following code counts the number of occurrences (> 1 if there are duplicates) given a specific value:

Code: Select all

--- Counts the number of occurrences of a given value
-- @param t Table
-- @param v Value
-- @return Number of occurrences
-- @return Total number of elements
function table.vcount(t, s)
  assert(s ~= nil, "second argument cannot be nil")
  local n = 0
  local d = 0
  for _, v in pairs(t) do
    if v == s then
      d = d + 1
    end
    n = n + 1
  end
  return d, n
end
Like I said, always a good idea to assert that the search needle is non-nil.
local t = { 'a','b','c','d','d','e','e','e' }
local found, total = table.vcount(t, 'd') -- found = 2, total = 8
table.vcount(t) -- error since the table cannot contain nil values

The following counts the number of unique elements (duplicates = total - unique):

Code: Select all

local _cache = {}

--- Removes all values
-- @param t Table
function table.clear(t)
  for i in pairs(t) do
    t[i] = nil
  end
end

--- Counts the number of unique elements
--- The number of duplicate elements can be calculated
--- by subtracting the first return value from the second
-- @param t Table
-- @return Number of unique values
-- @return Total number of elements
function table.ucount(t)
  -- clear the cache
  table.clear(_cache)
  local n = 0
  local d = 0
  for _, v in pairs(t) do
    if _cache[v] then
      d = d + 1
    else
      _cache[v] = true
    end
    n = n + 1
  end
  return n - d, n
end
This is useful when you want to make sure that there are no duplicate values within a table.
local t = { 'a','b','c','d','d','e','e','e' }
local unique, total = table.ucount(t) -- unique = 5, total = 8
assert(unique == total, 'the table contains duplicates')
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], slime and 15 guests