2 ms·
Lua tables are sort of two-headed beasts. They aren't like PHP's array construct, which is effectively always an ordered dict. In Lua, an ordered list and a dic
by cheald 15y ago
Lua tables are sort of two-headed beasts. They aren't like PHP's array construct, which is effectively always an ordered dict. In Lua, an ordered list and a dictionary are both expressed by the same type, but are effectively different types. You can even have both an ordered list, and a dictionary in the same table.
foo = {1,2,3}
This is an ordered list. #foo -> 3
foo = {foo = "bar", baz = "bin"}
This is a dictionary. #foo -> 0; the reason for this is that #foo (or more clearly, the length of foo) measures the length of the ordered list portion of a table. If you iterate an ordered list with ipairs(), you will only get the ordered list entries, even if you were to have dictionary data in the same table.
This is somewhat unintuitive in the context of other similar languages, but once you have a grasp of it, it makes a lot of sense, and is remarkably elegant.