Lua script , this is a key or index?

Viewed 45
a = {}
a["key"] = "value"
a[1] = "123"

Which one is the index , which one is key? I'm really confuse with this.

1 Answers

Lua does not differ between index and key. The manual uses both terms synonymously. Only if they refer to the C API they exclusively use index when they talk about integer values.

Among Lua users there is also a tendency to prefer index for integers and key for anything else.

That's probably because other languages that do not have associative array types use index exclusively.

You'll find that the manual and most users will use

for i, v in ipairs (t) end where only consecutive integer indices occur and for k, v in pairs (t) end where any type of key/index can occur. But of course you can name those variables whatever you want.

Related