Hi Guys!
I know you can use table.insert to add items to tables, but what about removing items from tables?
I am making a custom chat program and when a user leaves, i want their username to be erased from the users table
Thanks!
- House
Remove items from tables
Started by houseofkraft, Nov 07 2016 07:29 PM
5 replies to this topic
#1
Posted 07 November 2016 - 07:29 PM
#2
Posted 07 November 2016 - 07:33 PM
Have you considered table.remove?
#3
Posted 07 November 2016 - 07:49 PM
set the value to nil
#4
Posted 07 November 2016 - 08:11 PM
Oh yeah, i found out that i can remove tables with table.remove but it is for a certain number so i did this to get around it
local test = {"Test", "Again"}
for k,v in pairs(test) do
if v == "Again" then
table.remove(test, k)
end
end
#5
Posted 07 November 2016 - 09:18 PM
Another way of doing it is having the table formatted like this
list = {
["House"] = true,
["Anav"] = true,
}
If you want to remove a user from the list, simply do list[user] = nil
Edited by Anavrins, 07 November 2016 - 09:18 PM.
#6
Posted 07 November 2016 - 09:50 PM
houseofkraft, on 07 November 2016 - 08:11 PM, said:
Oh yeah, i found out that i can remove tables with table.remove but it is for a certain number so i did this to get around it
local test = {"Test", "Again"}
for k,v in pairs(test) do
if v == "Again" then
table.remove(test, k)
end
end
Note that for i = 1, #list is ordered and faster instead of pairs. If you don't want to get the value with local object = list[i] then you can use ipairs.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











