Index's and keys

local x={1,2,3}
x[0] = 5
-- is the x[0] a key or index?

Key, index are interchangeable really. It is just that people like referring to numerical keys as indexes and non-numerical keys as keys. The array part of a table always starts at 1 in Lua(u) so you have a mixed table on your hands. The index 0 gets added to the dictionary part of the table.

2 Likes

Thank you, are you absolutely certain and an expert, that is what i thought but someone I’m arguing with keeps saying otherwise lol

Also, since arrays in Lua start at 1, if you want to iterate through this table you would need to then use pairs or a numeric for loop starting at zero, since ipairs will only start at one.

1 Like

so is it a key or not lul just one word

There is your answer. They are interchangeable really.

no but in that script would it be considered a key or index, the guy I’m talking too keeps saying it’s an index

and only an index (just adding this so i can speak s ssssssssss)

It doesn’t matter, although like I mentioned people just like referring to numerical keys as indexes, anything else as just a “key”.

The index 0 will be added to the dictionary part of the table because Lua arrays start at 1

I’m confused cuz if i use an ipairs() loop on it, it doesn’t print it and ipairs() only prints index’s right?

ipairs only goes through the array part of a table, 0 is not picked up because it is in the dictionary part.

Rough implementation of ipairs in Lua:

function ipairs(a)
    return function(_a, i)
        i = i + 1

        local v = a[i]

        if v ~= nil then
            return i, v
        end
        return nil
    end, a, 0
end
1 Like

I think i understand, is it thought of as a index but it’s technically a numerical key?

1 Like

pairs will go through every key/value pair in a table, including the 0 index, though as @sjr04 said the 0 isn’t part of the array, so you can’t guarantee that 0 will be printed in order with the rest of the array elements. To ensure the proper ordering of indices you should iterate using a numeric for loop.

local x = {1, 2, 3}
x[0] = 5

for i, v in ipairs(x) do
	print(i, v)
end

--[[
will print:
1 1
2 2
3 3
]]

for i, v in pairs(x) do
	print(i, v)
end

--[[
will probably print:
1 1
2 2
3 3
0 5
]]

for i = 0, 3 do
	print(i, x[i])
end

--[[
will print:
0 5
1 1
2 2
3 3
]]

Actually ipairs{} == ipairs{} is true and if it encounters a nil it returns no value in Lua/Luau so a closer implemention to ipairs would be

local function inext(t, ...)
    if select('#', ...) == 0 then
        error("missing argument #2 (number expected)")
    end
    local i = (...)
    if not tonumber(i) then
        error("invalid argument #2 (number expected, got " .. typeof(i) .. ")")
    elseif type(t) ~= "table" then
        error("invalid argument #1 (table expected, got " .. typeof(t) .. ")")
    end
    i = i + 1
    if t[i] == nil then
        return
    end
    return i, t[i]
end
function ipairs(...)
    if select('#', ...) == 0 then
        error("missing argument #1 (table expected)")
    elseif type((...)) ~= "table" then
        error("invalid argument #1 (table expected, got " .. typeof((...)) .. ")")
    end
    return inext, ..., 0
end