Learn Lua · Tables · Lesson 2 of 5
Records
A table can also hold named fields, like a record: { name = "Ada", city = "Leeds" }.
Fields can hold other tables, so data nests:
local order = { customer = { name = "Ada" }, copies = 250 }
order.customer.name --> "Ada"In data your script receives, such as input, a field that isn't there reads as nil.
Compare with ~= nil to ask whether a field is present:
local input = ...
input.note --> nil
input.note ~= nil --> falseIn the Input line, note: string? says the same thing: the ? means the field may be
missing.
For a table you build yourself, the checker knows its fields, so reading one that doesn't exist is reported before the script runs.
What your code receives
Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.
input = {
order = {
customer = { name = "Ada" },
shipping = { city = "Leeds", country = "GB", street = "1 Mill St" },
},
}Steps
- Return
customer: the customer's name, frominput.order.customer.name. - Add
address: a record withcityandcountry, taken frominput.order.shipping. - Add
hasNote:truewhen the order has anote, otherwisefalse.
Show a hint
Build the address as a new table: { city = input.order.shipping.city, country = … }.