Team Spartan Cookies & Milk Forums
Non exists entry returns true - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: Non exists entry returns true (/thread-1780.html)



Non exists entry returns true - LocInt - 12-06-2016

Non exists entry return true on client side.

t[1]=true

after that I removed the entry and few seconds later I wanted to know if the record exists and it return true which is kinda weird since the table size prints as zero.


RE: Non exists entry returns true - Person8880 - 12-06-2016

Going to need to have more information than just t[1]=true.


RE: Non exists entry returns true - LocInt - 12-06-2016

This is how I perform the check.

i="Users"
window = {}
window.activeWindows = {}

Code:
if(not window.activeWindows[i]) then table.insert(window.activeWindows, i) window.activeWindows[i]=true end

This is how i removed the entry
Code:
local function removeEntry(t,entry) if(entry~=nil and #t>0) then for i=1, #t do if(t[i] == entry) then print(t[i]) table.remove(t,i) end end end end

After that I performed the first check again and it returned true even when the size was zero.


RE: Non exists entry returns true - Person8880 - 12-06-2016

In your loop:
for i=1, #t do
if(t[i] == entry) then
print(t[i])
table.remove(t,i)
end
end
The variable i is shadowing the value you defined above. When you use table.remove, it removes the value at the index i, which is a number due to the loop. This does not remove the value you stored under window.activeWindows[ "Users" ], hence you get true.

Also, table length through #Table measures the size of the array part of a table. That is, it counts the number of values in a sequence starting at index 1. Thus, non-array indices (such as strings) do not get counted.

You need to do:
t[ entry ] = nil
table.remove( t, i )
to remove both the value in the array part, and the string indexed value.


RE: Non exists entry returns true - LocInt - 12-06-2016

I tried to set the val as nil before i removed the entry but its still true.

Code:
t[i]=nil



RE: Non exists entry returns true - Person8880 - 12-06-2016

Read my answer again. In your remover function, i is coming from the loop, not your i = "Users" declaration above.


RE: Non exists entry returns true - LocInt - 12-06-2016

(12-06-2016, 09:13 PM)Person8880 Wrote: Read my answer again. In your remover function, i is coming from the loop, not your i = "Users" declaration above.

Nvm my bad.


RE: Non exists entry returns true - Person8880 - 12-06-2016

Tables are intentionally a multi-purpose data store. All the table.* functions are just helpers for treating tables as arrays. Same with the length operator.

Anything that is not to do with an array structure is just standard assignment/indexing. This leaves tables as being able to represent pretty much any data structure, which in the end is a good thing. Just have to watch for the difference between the array part, and the "dictionary" part.