Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Removing/Adding Values to Tables?solved

Asked by 6 years ago
Edited 6 years ago

Say I want to add a value to a table.

table = {}
table.insert(table, "remove me")
table.insert(table, "keep me")
table.insert(table, "1")
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)

for i, v in pairs(table) do
    if table[i] == "remove me" then
        table.remove(table, i)
    end
end
0
INTERESTING saSlol2436 716 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

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

for i=1, #table do
    if table[i] == "remove me" then
        table.remove(table, i)
    end
end

Also I don't recommend using "table" as the name of your table as it could cause some confusion

Hope this helped!

0
table is a syntax and you didn't even call the table saSlol2436 716 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

You did table.insert correctly.

mytable = {} -- table is a syntax
table.insert(mytable, "remove me")
table.insert(mytable, "keep me")
table.insert(mytable, "1")
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()

--Insert--
mytable = {} -- table is a syntax 
table.insert(mytable, "remove me")
print(mytable[1])
table.insert(mytable, "keep me")
table.insert(mytable, "1")
table.insert(mytable, "3")
--Remove--
wait(2)
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
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!

0
well.. I know how to use it I just didn't know how to find a certain value to remove if you didn't know the order. I'll upvote that though laughablehaha 494 — 6y
0
Thanks anyway saSlol2436 716 — 6y

Answer this question