This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
houseofkraft's profile picture

Remove items from tables

Started by houseofkraft, 07 November 2016 - 06:29 PM
houseofkraft #1
Posted 07 November 2016 - 07:29 PM
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
Anavrins #2
Posted 07 November 2016 - 07:33 PM
Have you considered table.remove?
Lupus590 #3
Posted 07 November 2016 - 07:49 PM
set the value to nil
houseofkraft #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
Anavrins #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 on 07 November 2016 - 08:18 PM
Sewbacca #6
Posted 07 November 2016 - 09:50 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

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 then you can use ipairs.