Simple question, but pretty sure that if index 1 of a table has a value then it automatically just puts it to index 2?
Yes, calling table.insert() with no index argument appends the desired value at the end of the array.
adding onto this, t[#t + 1]
still inserts values into the array-part of the table in Luau. This is because the #t
operation only returns the integer length of the array-part of the table, like so:
local t = {}
t[0] = 0
print(#t) -- 0
t[3] = 3
print(#t) -- 0
t[1] = 1
t[2] = 2
print(#t) -- 3
So, basically it’s just looping from 1 to #table
and ignoring the 0?
You could write your own ipairs() iterator which acknowledges array values located at index 0.
Correct. ipairs
has (mostly) the same behavior as
for i = 1, #t do
local v = t[i]
if v == nil then
break
end
-- code
end
Ohh, so it will keep returning 0 if the integers in the middle are nil
. I get it now!
Well, that’s good to know! Thanks a lot!
To answer your original question: t[#t + 1]
is an insertion method used outside of Roblox, but also in some old Roblox code. This practice is now considered bad, and you should always use table.insert
if you want to insert new values into an array.
Yes. I was referring to this variant (which also explicitly mentions ‘array’):
void table.insert(array t, number pos, Variant value)
From an indexing standpoint, an array can be thought of as a sequentially numbered dict. It can’t have gaps and prob needs to start at 1, but a table that’s set up that way can be treated as either an array or a dict.
Edit: (Will edit this rather than add another post)
The t[#t+1] = newVal
form adds an entry to the end of an array and is a technique used in other languages where arrays and dictionaries are completely different structures (so no ambiguity, and arrays are guaranteed to be well-formed). This approach can lead to confusion in Lua since it adds a new array entry using the Lua dict[key] = newVal
approach, but the values in table t are assumed to be, and must be, stored as an array for it to work (the #t doesn’t work correctly if the table isn’t an array). If you start removing values in your array with the similarly structured t[n] = nil
assignment, you can break the array.
Obv we’re using t[index|key] = updateVal
to make changes to existing entries either way, but using the t[key] = newVal
and t[key] = nil
approach when you need to add/rem a dict entry and the table.insert(t, newVal)
and table.remove(t, remIndex)
when you need to add/rem an array entry is best practice (as Pyseph notes) since it avoids ambiguity, and the later table methods ensure that your arrays stay well-formed.
I’m not going to be choosing any posts as a solution, because all the posts has explained a lot! (Anybody who sees this post sometime later, should just check this full thread, so you know more!)
Thanks a lot everybody!
Open for extra information
So, mainly the full post explained that
table[#table + 1]
is a bad an old practice, which you can achieve using table.insert()
which Astr0 showed in reply before this one!
If you define a key then it becomes a dictionary but the [] can be used for both arrays and dictionaries. It’s used to set and read the data at that key or index.
table.insert() does not work on dictionaries, I believe you may be confused.
I’m not saying it does, I’m saying it dosent.
[1] = "notfunnymemeamogus"
With that table.insert method
This post was very unclear.
local dictionarytable = {
[1] = "good time to use their method"
}
and this isn’t an example of a dictionary.
table.insert is a function call.
The roblox script thing looks for table.insert value in the global memory area and then calls it.
[#table + 1] is much faster
There’s nothing such as dictionaries in luau, they are tables with non-numeric indexes. So no, this is incorrect, table.insert()
will actually work on “dictionaries” since they can have both numeric and non-numeric indexes at the same time.
Also the difference between table.insert()
and table[#table + 1]
is that with table.insert()
, you’ll always start from the last numeric index (or 0 if the table has none) and with table[#table + 1]
you’ll always start from the length of the table, not the last numeric index.
They are exactly the same.
Code
local insert_table = {}
local index_table = {}
local insert_start = os.clock()
table.insert(insert_table, "a")
print("table.insert took: " .. os.clock() - insert_start)
local index_start = os.clock()
index_table[#index_table + 1] = "a"
print("t[#t + 1] took: " .. os.clock() - index_start)
Not to discredit those results, but typically comparing the time taken to execute calls is done over millions of iterations and not just one-time calls.
Well it’ll probably still remain very close since both are O(1) and are precisely the same
w/ 10000 iterations
(I’m actually surprised that table.insert
is a bit faster)