Difference between "key" and "index"?

Hello,
I am a developer who alternates between several languages (python, nodejs, glua and roblox lua).
So I’m a bit lost between the terms used in the different languages.

I recently wanted to learn how to use metatables and I’m confused with the term “index”.
for me an array is made of two things :
- a key
- a value
This makes sense in for loops:

for key, value in pairs(table) do

end

Thank you for helping me

In a metatable, an index is what you’re trying to index the table with.

Take for example this:

local mt = setmetatable({}, {
   __index = function(self, index)
       if index == "something" then
           return "new"
       end
   end)
})

print(mt['something']) -- prints "new"

The index in a metatable function parameter is ultimately what is being indexed, with the index metamethod being used as an example above. The “self” parameter is ultimately the metatable itself - if you’re familiar with object oriented programming, this is basically the equivalent of the “this” keyword when referencing something inside a class (specifically C++, or some other languages).

Edit:

Roblox has some really good documentation in regards to this specifically, you should check it out:

1 Like

There isn’t really a difference, they’re two words for the same concept.

In practice, “index” is usually used in arrays like

local array = {"a", "b", "c"}
local index = 2
print(array[index])

whereas “key” is usually used in associative dictionaries like

local dict = {first = "a", second = "b", third = "c"}
local key = "second"
print(dict[key])

But they’re just names.

In lua both terms can generally be used interchangeably, though generally you should use the term “index” when refering to array-like tablestructures and “key” when refering to hashmaps.

Ahhhh so if I understand correctly, unlike python, lua does not categorize arrays right?

for key, value in pairs(table) do
  -- for everything in the table, this loop will run.

  -- the value, is the value the loop is running on, hard to explain so far, let me give you an example
end

So, we will run over a table of items

local list = {'Hello', 'Welcome', true, false, 10, 23}
for key, value in pairs(list) do
  -- this loop will run for everything in the table, in the order we set the table
  -- first it will run for 'Hello', in this case, key is 1, and the value is 'Hello', 'Hello' is the first thing in the table, and 'Hello' is equal to 'Hello'

print(key) -- prints the index of the item in the table, so since 'Hello' is first, it will print 1, since it's the first thing in the table
print(value) -- prints 'Hello' since that is the value of 'Hello'

-- now the script runs for the NEXT item in the table, and it goes on like that

end

Not really sure what you mean by “categorize”, but you can read more about arrays in lua in Programming in Lua (first edition).

See chapters 2.5 and 11.

Essentially one way to think of it actually (when you mentioned metatables I thought you were talking about those) - the term “key” and “index” are the same, and value is the same.

Lua also calls arrays “tables”. Lua’s tables are what arrays are in other languages, they are essentially the same just without specific types associated between them (like, how C++ has hash maps and vectors, etc)

Thank you very much :grin: !
Now I understand a little better.

1 Like