- hello-world - hello world
- comments
- factorial - simple resursive function, user input
- control-structures -
if/else
, while
, for
, etc.
- dofile - run a chunk from another file
- types - basic types:
nil
, boolean
, string
, userdata
, function
, thread
, table
. Also integer
vs float
- math - examples from
math
library
- strings - examples, along with
string
library examples
- tables - lists, records, arrays
- functions
- io - Simple I/O, Complete I/O, basic file read/write
- os - Operating System,
os
library, to interact with machine, date
- pattern-matching - strings: finding, replacing, matching
- binary - pack and unpack data to binary
- Serialization (skipped)
- compilation
- errors - and how to handle them
- modules - modules, packages, and
require
- iterators - going beyond for loop
- eight-queen - examples of functions to solve a chess problem
- word-frequency - find most frequent words in a text
# enter code directly into command line
lua -e 'print("hello world")'
# interactive mode
lua -i
- Tables
{}
are the main data structure. They can be used as lists, sequences, arrays, etc.
- There is no
switch
. use elseif
instead
- There is a
goto
. See control-structures
- Functions are first-class values
- they have the same rights as other types like strings, and can be stored as variables or in tables
- Functions have lexical scoping - can access variables within their enclosing function
and | elseif | function |
local | repeat | until |
break | end | goto |
nil | return | while |
do | false | if |
not | then | |
else | for | in |
or | true | |
< | less than | <= | less than or equal to | == | equal to |
> | greater than | >= | greater than or equal to | ~= | not equal to |
- Capitalize to represent the opposite, e.g.,
%A
is all non-letter characters.
. | all characters |
%g | printable except spaces |
%u | uppercase |
%a | letters |
%l | lowercase |
%w | alphanumeric |
%c | control characters |
%p | punctuation |
%x | hexadecimal |
%d | digits |
%s | spaces |
| |
Precompiled code is not always smaller, but it loads faster and it serves as a protection against accidental changes
# -o means create a new file
luac -o hello-world.lc hello-world.lua
# precompiled code can run with lua command
lua hello-world.lc
# Add LUA_PATH environmental variable to .bash_profile
export LUA_PATH="~/lua/?.lua;;"
# confirm with printenv LUA_PATH