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:

LUA
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:

LUA
local input = ...
input.note         --> nil
input.note ~= nil  --> false

In 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.

LUA
input = {
  order = {
    customer = { name = "Ada" },
    shipping = { city = "Leeds", country = "GB", street = "1 Mill St" },
  },
}

Steps

  1. Return customer: the customer's name, from input.order.customer.name.
  2. Add address: a record with city and country, taken from input.order.shipping.
  3. Add hasNote: true when the order has a note, otherwise false.
Show a hint

Build the address as a new table: { city = input.order.shipping.city, country = … }.