Why do some people use table[index + 1] instead of table.insert?

Note that the table.insert() requires a numerical index when you are specifying where to insert something, so it’s intended for use with arrays as bindableunderscore said. There is overlap in functionality since arrays and dicts are both lua tables. Either approach works if it does what you need.

Yeah, that’s right. (not funny meme amogus-)

1 Like

it’s really not funny lol

3 Likes

Internally, every table has an array and hashmap portion, and so a table can be both a dictionary and an array at the same time.

local t = {}

-- insert a value into the hashmap (ie., dictionary) part of the table
t[0] = "dictionary value"

-- insert values into the array part of the etable
for i = 1, 5 do
    table.insert(t, i)
end

-- this will print all values of the array-part of the table, ignoring the value at index 0.
for i, v in ipairs(t) do
    print(v)
end

-- this will the value stored in the hashmap part of the table, "dictionary value".
print(t[0])
3 Likes

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.

1 Like

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
1 Like

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.

1 Like

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
1 Like

Ohh, so it will keep returning 0 if the integers in the middle are nil. I get it now!

1 Like

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.

1 Like

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.

1 Like

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