Tables¶
Defining tables¶
Tables in Hurdy work the same as in Lua and are defined in the same way:
var t = {
1, 2, 3,
s = 5,
[8] = 7,
["end"] = true
}
-- defines the table with
-- t[1] = 1
-- t[2] = 2
-- t[3] = 3
-- t[8] = 7
-- t["s"] = t.s = 5
-- t["end"] = true
The only difference with Lua is that both commas and newlines can be used as separators (mixing the two syntaxes is allowed):
var t = {
1
2
3
}
is equivalent to
var t = {
1,
2,
3
}
Trailing commas are allowed and are ignored.
Accessing tables¶
Tables (and in general objects with an __index
metamethod) can be accessed in the same way as Lua:
t.identifier
t["identifier"]
t:identifier() -- can only be used to call a method and is equivalent to t.identifier(t)
Note
Just as in Lua, when accessing a table using the .
or :
operator is only possible if
the identifier is not a Lua reserved keyword. This restriction does not apply to Hurdy-only keywords
(e.g., var
).
t.end -- not allowed
t["end"] -- this is ok
t:global() -- this is ok
This restriction may be removed in the future.