|
| 09 Jul 2014 09:21 AM |
| How do I remove a specific element from a table without knowing its position in the table and knowing its value? |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:23 AM |
function getIndex(tab, value) if type(value) == "string" then value = value:lower() end for k,v in pairs(tab) do if type(v) == "string" then v = v:lower() end if v == value then return k end end return nil end |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:26 AM |
D: Please do explain. And the element that I want to remove is not a string, it's an instance. |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 09 Jul 2014 09:28 AM |
table_remove = function(_table, _value) for index, value in pairs(_table) do if value == _value then table.remove(_table,index) return true end end return false end
local t = {1,2,3,"hello"} table_remove(t,"hello")
should work.
Second poster modifies strings which is stupid for something like this |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:28 AM |
tab = {game.Players.Duelingwarlord}
function getIndex(tab, value) if type(value) == "string" then value = value:lower() end for k,v in pairs(tab) do if type(v) == "string" then v = v:lower() end if v == value then return k end end return nil end
table.remove(tab, getIndex(tab, game.Players.Duelingwarlord))
|
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:30 AM |
How would I know what type of table it is. He tells me after the fact. There is something called reading before you type. Learn to do that before you throw words around that make you look brainless. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:30 AM |
oh and hey you used exactly the same thing I did. you must be stupid as well :p |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 09 Jul 2014 09:31 AM |
| I just don't modify the strings. |
|
|
| Report Abuse |
|
|
| |
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 09 Jul 2014 09:33 AM |
| Your script is more stupid than mine. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:36 AM |
no urs is, he didn't tell me what type he wanted. so I assumed strings and not instances. Mine works for strings AND instances
so me > u
|
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 09 Jul 2014 09:48 AM |
Mine supports strings, instances, numbers, booleans ect.. and it doesnt mess up strings. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:52 AM |
local get_index=function(t,val) local index=nil for i,v in ipairs(t) do if val:lower()==v:lower() then index=i end end return index end
local ex={'Hi','Two',2,1.1,'L','A'} local index_1=get_index(ex,'Two') local index_2=get_index(ex,2) table.remove(ex,index_1) table.remove(ex,index_2) print(table.concat(ex,', ')) |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:56 AM |
| Boys, let's not fight over this. |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:57 AM |
I can remove 2 lines and it'd be better than yours still. so why are you so upset? |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 09:59 AM |
| lol tri is trying so hard 2 be a nerd xdddd |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 09 Jul 2014 10:20 AM |
Duel, calm down man. All I see you do is get into arguments on these threads.
Also, what I do is this:
table = {"What", "You", "Had"}
randnum = math.random (1,#table) table.remove (table,randum) |
|
|
| Report Abuse |
|
|
|
| 09 Jul 2014 11:49 AM |
| Other people provoke these arguments + you did it wrong. |
|
|
| Report Abuse |
|
|