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

Any other way to remove a value from a table using table.remove?

Asked by 4 years ago

I am just wondering if there is any other way to remove a value from a table not using table.remove().

I haven't really played with anything but here was my thought, create a new table and transfer those values to that table and get rid of it later on.

local function remove(tab, index)
    local placeholder = {}
    for i,v in pairs(tab) do
        if i == index then
            table.insert(placeholder, v)
            -- Needs to use table.remove to remove the selected value
        end
    end
end

Problem is there is no way to transfer a value like that without using table.remove() .

3
table[key] = nil herrtt 387 — 4y
2
table.index = nil herrtt 387 — 4y

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can do this:

table = {1, 2, 3
} -- set a table that there have 1, 2, 3 values.

newtable = {
} -- create a new table

newtable.insert = table[1] -- insert the table's first value inside the new table
newtable.insert = table[2] -- insert the table's second value into the new table

table = nil -- set the table to nil, so the table might clear
print("table's third value have been removed")

Simply, it works like this. My script is not 100% correct, this is just a concept for you. If this helped you or anything, please upvote! I don't like to say it, but it helps a lot! Thanks :D

0
sorry i took long i was inactive 123nabilben123 499 — 4y
0
nevermind. Xapelize 2658 — 4y
Ad
Log in to vote
2
Answered by 4 years ago

Like herrtt is saying, you could just set the value of the slots in your table to nil.

--placeholder is your table. Let's say you have these values in your table:
{1, banana, 64, $, 9}
--If you want to get rid of banana, you would use
placeholder[2] = nil
--You use the 2 because banana is the second value in the table.
Now your table looks like this:
{1, nil, 64, $, 9}
--Remember that nil is not a string value itself; it's the absence of a value.

Hope I helped, and happy coding!

0
Something to keep in mind when is this method: you aren't really removing the value from the table but replacing it with nil. ScuffedAI 435 — 4y
0
Also, the length of the table will still remain the same as previously. This might not be desirable in some cases. ScuffedAI 435 — 4y

Answer this question