local in Lua
local scopes a name to its block; bare assignment goes global.
-- local scopes a name to its block; bare assignment goes global.
local hero = "Wren"
local hp = 20
do
-- A do block opens a fresh scope; this hp shadows the outer one.
local hp = hp + 5
print(hero .. " drinks a tonic: " .. hp .. " hp inside the vault")
end
print("Back outside, hp is still " .. hp)
-- Multiple assignment binds left to right.
local gold, gems = 120, 3
print(string.format("Purse: %d gold, %d gems", gold, gems))
How it works
local herolives to the end of its enclosing block.- A
do ... endblock opens a fresh scope for shadowing. local gold, gems = 120, 3assigns multiply, left to right.
Keywords and builtins used here
doendlocalprint
The run, in numbers
- Lines
- 15
- Characters to type
- 432
- Tokens
- 63
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 54 seconds.
Step 2 of 3 in Values, step 2 of 27 in Language basics.