Hello all!
I am starting to learn how to use tables, and I was wondering if there was any possible way to reset a table after everything has been removed.
So when #Table == 0 it resets the table back to what it originally was when the script was first run.
Thank you in advance for your help!
You could do this by keeping an untouched copy of the initial table somewhere then copy all of it's elements to a new table:
local table = { "InitialValue1", "InitialValue2" } local function copyTable(tableToCopy) local newTable = {} -- We iterate, or *browse*, through every element from table and copy them -- to newTable. for i, v in pairs(tableToCopy) do newTable[i] = v end -- Another way to this, and probably a faster one, would be to unpack the -- values of the first table into a new one: -- -- local newTable = { unpack(tableToCopy) } -- -- The 'unpack()' function takes a table as first argument and returns a -- comma-separated list of all it's elements. So we "catch" them inside -- a new table. return newTable end local function removeElement(table, index) table.remove(table, index) -- Immediately after removing an element, we check if the table is empty. if #table == 0 then -- If it is then we copy the elements over from the original one again. if #newTable == 0 then newTable = copyTable(table) end end end -- We copy the original table to a new table. local newTable = copy(table) -- Later... -- We remove all the elements from "newTable" using our custom -- "removeElement()" function to ensure our empty check will always be -- done immediately after the removal of an element. removeElement(removeElement, 2) removeElement(removeElement, 1) -- At this point in time, newTable should have been reinitialized with it's -- default values. We can verify this by iterating, or *browsing* through each -- of it's elements and print their value: for i, v in pairs(newTable) do print("newTable[" .. i .. "] = " .. tostring(v)) end
Note that in this example comparing table
with newTable
, E.g:
if table == newTable then -- Do something... end
Will always return false
because tables are compared by reference.
This topic is a bit more advanced for this answer but basically, every variable has a value and an unique address location in the computer's memory. To comparing tables (unlike numbers, strings and the nil
value) is to compare their addresses instead of their value. Since each table has its own unique address, and since they are compared by address, comparing two variables with table values serve to know whether the two variables point to the same table in memory.
About References:
A variable that points to a value is known as a reference or a pointer. The difference between a reference and a value-type is that since multiple references can point to the same block of memory (to the same value in memory), then modifying the value through one reference (or pointer) will replicate the changes to any other reference. To simplify:
-- We create a new table and assign it's address to a reference (or pointer) "foo". local foo = {} -- We introduce a new reference (or pointer) value "bar" and assign -- it the same address as foo. local bar = foo -- We modify the table pointed to by the reference (or pointer) "foo'". foo[1] = "Hello, World!" -- The changes to foo are replicated to bar. print(bar[1]) -- Will print "Hello, World!".
For the same reason, you cannot copy a table by simply assigning a variable to it (E.g: local newTable = table
); since it is passed by reference instead of by value.
Hope this helps. If it does, please make sure to vote the answer up and to accept it. :) If you need anything else, feel free to leave me a message.
local tableName = {"x","y","z"} local tableNameCopy = tableName while true do if tableNameCopy < 1 then tableNameCopy = tableName end wait() end