K4stic Posted January 29, 2014 Share Posted January 29, 2014 Hello MTAsa Community i have 2 Questions 1. if i have a table and i was add a line intro it this will remove it? testtable[name] = nil if No them how remove it because table.remove return me arithmetic error local testtable = { } function ADDtoTable( name ) testtable[name] = { "test" } end function RemovefromTable( name ) testtable[name] = nil end 2. if i have lot added to that table thinks how i can find if that table avaible like i want find if avaible the table testtable[name.."2"] local testtable = { } function ADDtoTable( name ) testtable[name] = { "test" } testtable[name.."1"] = { "test" } testtable[name.."2"] = { "test" } testtable[name.."3"] = { "test" } end Thanks for your Time Awaiting your Responce Link to comment
Moderators IIYAMA Posted January 29, 2014 Moderators Share Posted January 29, 2014 local index = 1 while true do if not testtable[name.. index] then testtable[name.. index]=variable break end end Link to comment
K4stic Posted January 29, 2014 Author Share Posted January 29, 2014 local index = 1 while true do if not testtable[name.. index] then testtable[name.. index]=variable break end end Thanks you but you answer me to 2nd question only Link to comment
Moderators IIYAMA Posted January 29, 2014 Moderators Share Posted January 29, 2014 You mean removing it? local index = 1 while true do if testtable[name.. index] then testtable[name.. index]=nil else break end end Same way. But this is better and faster. function RemoveFromTable( name ) if testtable[name] then testtable[name] = nil end end function RemoveVariableFromTable( name,variable ) local table = testtable[name] if table then for i=1,#table do if table[i]== variable then table.remove(table,i) break end end end end function ADDtoTable( name,variable ) local table = testtable[name] if table then table[#table+1]=variable else testtable[name] = { variable } end end Link to comment
K4stic Posted January 29, 2014 Author Share Posted January 29, 2014 Thanks You IIYAMA! Link to comment
K4stic Posted January 29, 2014 Author Share Posted January 29, 2014 sorry for Duble pos but if i want remove it full i mean like testtable[name.."2"] <= i want remove it not just the variable's on it Link to comment
Moderators IIYAMA Posted January 29, 2014 Moderators Share Posted January 29, 2014 this is removing it: testtable[name.. index]=nil table.remove Removes the variable from the array position, so the array stays clean from empty places. myTestTable={"a","b","c","d"} --is the same as: myTestTable={ [1]="a", [2]="b", [3]="c", [4]="d"} myTestTable={ [1]="a",-- [1] array location 1 [2]="b", [3]="c", [4]="d"}-- [4] array location 4 table.remove(myTestTable,2) --result: myTestTable={ [1]="a", [2]="c", [3]="d"} Since you don't have a clear array based on numbers, you can't use table.remove. Or you have to do it like this: function RemoveFromTable( name ) if testtable[name] then testtable[name] = nil end end function RemoveVariableFromTable( name,variable ) local table = testtable[name] if table then for i=1,#table do if table[i]== variable then table.remove(table,i) break end end end end function ADDtoTable( name,variable ) local table = testtable[name] if table then table[#table+1]=variable else testtable[name] = { variable } end end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now