Three things about Lua strings will surprise you if you have written JavaScript or Python. You join them with .. rather than +, you measure them with # rather than a length property, and they are counted from 1.
local first = "Ada"local last = "Lovelace"
print(first .. " " .. last)print(#first)print(first:upper())print(last:lower())Ada Lovelace3ADAlovelaceLua uses a separate operator for joining because + is reserved for arithmetic and nothing else. If you have ever been bitten by JavaScript quietly turning "16" + 1 into "161", this is the design that prevents it.
first:upper() is shorthand. Every string function really lives in a library called string, and the colon just passes the string in as the first argument. These two lines are the same line.
local unit = "reactor"
print(string.upper(unit))print(unit:upper())REACTORREACTORWorth filing away: the colon is not a string feature. It is a general Lua mechanism, and in Activity 1.11 you will use the same syntax to build your own objects.
Chaining .. works, and numbers convert themselves along the way. It also gets ugly fast, which is what string.format is for.
local unit = "Reactor"local charge = 98
print(unit .. " is at " .. charge .. " percent")print(string.format("%s is at %d%% charge", unit, charge))Reactor is at 98 percentReactor is at 98% chargeThe placeholders are %s for a string and %d for a whole number, filled in by the arguments that follow, in order. To print an actual percent sign you write %%, since a lone % means a placeholder is coming.
sub cuts out a piece, given a start and an end position. Both ends are included, and counting starts at 1 — not 0. A negative number counts back from the end.
local code = "SKYNEST"
print(code:sub(1, 3))print(code:sub(-4))print(code:sub(2, 2))print(code:find("NEST"))SKYNESTK4 7That last line returns two values — where the match starts and where it ends. Lua functions are allowed to hand back more than one thing, which is unusual and extremely handy. Activity 1.7 covers it properly.
The 1-based counting is not a string quirk. Everything in Lua that has positions counts from 1, including tables. Getting used to it here makes Activity 1.9 much less startling.
Double square brackets hold a string exactly as you typed it, line breaks and all, with no escaping.
local banner = [[+------------------+| SKYNEST v1.0 |+------------------+]]
print(banner)+------------------+| SKYNEST v1.0 |+------------------+A line break immediately after the opening [[ is skipped, which is why the banner above does not start with a blank line.
Start a fresh main.lua. You are building a crew badge line, the kind of thing a game would print above a character.
-- main.lua - Skynest crew badge
local first = "Ada"local last = "Lovelace"
local fullName = first .. " " .. lastprint(fullName)print(#fullName)Ada Lovelace12string.format to print one line containing all three.-- main.lua - Skynest crew badge
local first = "Ada"local last = "Lovelace"local role = "engineer"local charge = 98
local fullName = first .. " " .. lastprint(fullName)print(#fullName)
print(string.format("%s (%s) - reactor at %d%%", fullName, role:upper(), charge))last:sub(1, 3):upper() works, because sub hands back a string and a string has an upper.-- main.lua - Skynest crew badge
local first = "Ada"local last = "Lovelace"local role = "engineer"local charge = 98
local fullName = first .. " " .. lastprint(fullName)print(#fullName)
print(string.format("%s (%s) - reactor at %d%%", fullName, role:upper(), charge))
local code = last:sub(1, 3):upper() .. "-" .. first:sub(1, 1):upper()print("Badge ID:", code)[[ ]] string.print(#"café") and see whether you get what you expected. # counts bytes, and some characters take more than one.Your finished file should produce this.
Ada Lovelace12Ada Lovelace (ENGINEER) - reactor at 98%Badge ID: LOV-AIf your badge came out as LOV-A without you counting a single character by hand, you have the idea. If it came out one letter short or long, you have met the 1-based counting for real, which is a better way to learn it than being told.