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

Hey there people! Yep, another question.

Why do people use table[index + 1] = val instead of table.insert()? And it’s not only that. People also use table[index] = nil or something like that. But why? Is it more efficient than table.insert() or table.remove()? Thanks for answering!

A little more information. If you’re confused on what I’m trying to say, this is what I mean:

for index = #myTable, 1, -1 do
    myTable[index + 1] = "Hello world!"
end

Something like that.

2 Likes

Table.insert is for arrays.

While their method is for dictionaries.

Example:

local arraytable = {"goodtimetousetableinsert"}
table.insert(arraytable,"yes,goodtime")
local dictionarytable = {
[1] = "good time to use their method"
}
for index = #dictionarytable, 1, -1 do
    dictionarytable[index + 1] = "yeah, good time"
end
1 Like

Ohhh, I see. I never knew that! Thanks a lot!

1 Like

That second example is still an array.

2 Likes

I believe it’s still in dictionaries, just in numbers

table.insert(tables, value)

Is the same as:

tables[#tables + 1] = value

The difference is purely down to preference.

3 Likes

Just that you can do it in tables like [1],[2] etc

Arrays are indexed via sequential numbers starting from 1 automatically.
local dictionarytable = {[1] = "good time to use their method"}
Is the same as:
local dictionarytable = {"good time to use their method"}

4 Likes

Yeah, pretty sure they’re the same thing, because I can just do this right?

local dictionaryTable = {
    "Hello 1",
    "Hello 2",
    "Hello 3";
}

Which will be the same thing as [1] = "Hello 1" and like that. (I think…)

Yes, forummer is right but their table.insert method can also do stuff like

[1] = "notfunnymemeamogus"

With that table.insert method

1 Like

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