• Remove Something from a Table by Value?
    5 replies, posted
Is there a function like [lua] table.RemoveValue(table table, string/integer/boolean value) [/lua] ? For removing a value by its name.
[b][url=http://wiki.garrysmod.com/?title=Table.remove]Table.remove [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[QUOTE=Teddi Orange;29049842][b][url=http://wiki.garrysmod.com/?title=Table.remove]Table.remove [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b][/QUOTE] No. He's asking about removing by value, not by key. [lua]for k, v in pairs( tbl ) do if v == value then tbl[k] = nil end end[/lua]
[QUOTE=raBBish;29050434]No. He's asking about removing by value, not by key. [lua]for k, v in pairs( tbl ) do if v == value then tbl[k] = nil end end[/lua][/QUOTE] [b][url=http://wiki.garrysmod.com/?title=Table.KeyFromValue]Table.KeyFromValue [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] is a shortcut for looping through the table.
[QUOTE=PortalGod;29056865][b][url=http://wiki.garrysmod.com/?title=Table.KeyFromValue]Table.KeyFromValue [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b] is a shortcut for looping through the table.[/QUOTE] That would work if he only wanted to remove only one of the specific value, I assumed he wanted to remove every element with the value.
Just one, actually, but thanks for the help. Its for an inventory script, so I can use what is returned by that in table.remove.
Sorry, you need to Log In to post a reply to this thread.