Jump to content




Remove items from tables


  • You cannot reply to this topic
5 replies to this topic

#1 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

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

#2 Anavrins

  • Members
  • 775 posts

Posted 07 November 2016 - 07:33 PM

Have you considered table.remove?

#3 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 07 November 2016 - 07:49 PM

set the value to nil

#4 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

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 Anavrins

  • Members
  • 775 posts

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 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 07 November 2016 - 09:50 PM

View Posthouseofkraft, 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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users