Say I want to add a value to a table.
1 | table = { } |
2 | table.insert(table, "remove me" ) |
3 | table.insert(table, "keep me" ) |
4 | table.insert(table, "1" ) |
5 | table.insert(table, "3" ) |
Now I want to remove a certain value from a table.(pretend I don't know the order of the table)
1 | for i, v in pairs (table) do |
2 | if table [ i ] = = "remove me" then |
3 | table.remove(table, i) |
4 | end |
5 | end |
I don't know why but when searching through a table like this where the values aren't set to something else I prefer to sue this method
1 | for i = 1 , #table do |
2 | if table [ i ] = = "remove me" then |
3 | table.remove(table, i) |
4 | end |
5 | end |
Also I don't recommend using "table" as the name of your table as it could cause some confusion
Hope this helped!
You did table.insert correctly.
1 | mytable = { } -- table is a syntax |
2 | table.insert(mytable, "remove me" ) |
3 | table.insert(mytable, "keep me" ) |
4 | table.insert(mytable, "1" ) |
5 | table.insert(mytable, "3" ) |
Now how do we do table.remove?? table.remove() has two arguments. The first one is the table. The next one is the index. In the second script you will see how to use table.remove()
01 | --Insert-- |
02 | mytable = { } -- table is a syntax |
03 | table.insert(mytable, "remove me" ) |
04 | print (mytable [ 1 ] ) |
05 | table.insert(mytable, "keep me" ) |
06 | table.insert(mytable, "1" ) |
07 | table.insert(mytable, "3" ) |
08 | --Remove-- |
09 | wait( 2 ) |
10 | table.remove(mytable, 1 ) -- The first index was a string and it was "remove me" right?? Now it will be deleted from the table myTable |
11 | print (myTable [ 1 ] ) -- Now the first index is now the string "keep me", So the results are now supposed to be "keep me" |
Hopefully that is a very detailed answer to your question!! Please submit this answer if it worked!