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

What is the best way to clear out all entries in a Dictionary?

I have some code that will overwrite on an as-needed basis for MANY dictionary entries. Is this the correct way to go about this if I want to make sure all memory is released for the entries:

--Script

local module = require(script.ModuleScript)
local done = module.Add()
local myTable = module.Table()

function removeTable()
	for i, v in pairs(myTable) do
		print(i,v)
	end
	myTable = module.Table(true)
	print("---table removed checking---")
	for i, v in pairs(myTable) do
		print(i,v)
	end
	print("---table checking done ---")
end

removeTable()

done = module.Add()
for i,v in pairs (myTable) do
	print(i,v)
end

. .

--Module Script

local module = {}
local theTable = {}

function module.Add()
	local counter = 0
	local temp = ""
	while counter < 3 do
		counter = counter + 1
		temp = tostring(counter)
		theTable[temp] = true
	end
	return (theTable)
end

function module.Table(valueIn)
	if valueIn == true then
		theTable = {}
	end
	return (theTable)
end

return module

--[[   Output
	
1 true
3 true
2 true
---table removed checking---
---table checking done ---
1 true
3 true
2 true
--]]

It looks like it works how I want it to, but I'm still learning and I really don't want to deal with a problem later on.

1 comment

0

Ok perfect, thank you! I saw lots of info on how to remove individual entries, yet none on releasing an entire table (with A LOT of entries).

2 answers

✓ Accepted answer

There are two ways of doing this, redefining it as an empty table or using a generic for loops to essentially remove each entry

Ex1, redefining as an empty table:

local dict = {oof = "ree", Ding = "Pong"}
print(dict.oof)--"ree"
dict = {}
print(dict.oof)--nil

Ex2, generic for loop

local dict = {oof = "ree", Ding = "Pong"}
print(dict.oof)--"ree"

for k in next,dict do
	dict[k] = nil
end
print(dict.oof)--nil

Hopefully this helped you out

1 comment

0

Thank you

What you are doing is creating a new table when you run module.Table(true) this means that anything held within the last table can be used.

Example

local tblModule = require(script.ModuleScript)

local tbl1 = tblModule.Table()
local tbl2 = tblModule.Table(true)
print(tbl1 == tbl2) -- false

This can cause a lot of problems if two scripts use this same module they could end up with different tables being used.

The valid method of clearing a index is to set its value to nil tbl1["2"] = nil. So your clear function should loop through the table and set the key to nil.

function module.Table(valueIn)
    if valueIn == true then
        for i, _ in pairs(theTable) do
			theTable[i] = nil
		end
    end
    return theTable -- parentheses are not needed here
end

The second problem is with your Add function as running it again will have 0 effect on the table as the counter is not stored. You should also use a numeric for loop as well.


local counter = 0
local addAmount = 3
local theTable = {}

function module.Add()
	for i=1,   addAmount do
		theTable[tostring(counter +i)] = true
	end

	counter = counter  + addAmount 
end

You would also need to reset the counter when you clear the table.

Hope this helps.

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