Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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() .

2 comments

3

table[key] = nil

2

table.index = nil

2 answers

✓ Accepted answer

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

2 comments

0

sorry i took long i was inactive

0

nevermind.

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!

2 comments

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.

0

Also, the length of the table will still remain the same as previously. This might not be desirable in some cases.

This discussion is preserved as it was. New answers, comments, and votes are closed.