-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lua_demo.lua
73 lines (59 loc) · 1.09 KB
/
lua_demo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--
-- Variables
--
-- global (accessible from other Lua modules)
hello = 'hello'
-- local (accessible only in this scope)
local world = ' world!'
--
-- Functions
--
-- declaring our function
function say(text)
print(text)
end
-- calling our function (note the .. operator to concatenate strings!)
say(hello .. world)
--
-- If statements
--
if world == 'world' then
print('world!')
else
print('hello!')
end
--
-- Loops
--
-- while loop with counter
local i = 10
while i > 0 do
-- note the lack of -= and +=
i = i - 1
print(i)
end
-- for loop, decrements from 10 to 1
for j = 10, 1, -1 do
print(j)
end
-- repeat (do-while) loop
i = 10
repeat
i = i - 1
print(i)
until i == 0
--
-- Tables
--
-- sort of like structs or hash tables in C, and like Python dictionaries
local person = {}
person.name = 'Colton Ogden'
person.age = 26
person.height = 69.5
-- bracket and dot syntax to access table fields
print(person['name'])
print(person.name)
-- iterate through table's key-value pairs and print both of each
for key, value in pairs(person) do
print(key, value)
end