Template repo for tiny cross-platform apps that can be modified on phone, tablet or computer.
1{
2 {type='rows', default={font={size=21}, line_height=25},
3 {type='text', font={size=28}, line_height=50,
4 {'Lua@{Lua} > tables',
5 attrs={
6 Lua={target='--lua'},
7 }},
8 },
9 {type='filler', height=15},
10 {type='text', "Lua's tables are a versatile way to group data together in ways that make it easy to slice and dice."},
11 {type='filler', height=15},
12 {type='text', '{} is an empty table value/expression.'},
13 {type='filler', height=10},
14 {type='text',
15 {'h = {11, 12, 13} creates an em@{array}, a table that can be looked up by number em@{keys}.',
16 attrs={
17 em={font={italic=true}}}}},
18 {type='text', ' h[1] is now 11, h[2] is 12 and so on.'},
19 {type='filler', height=10},
20 {type='text', 'h = {a=34} creates a table that can be looked up by string keys.'},
21 {type='text', " h['a'] is now 34, h['b'] is 35."},
22 {type='text', ' You can also shorten constant string keys using periods. h.a is 34, h.b is 35.'},
23 {type='filler', height=10},
24 {type='text', 'if h is an array. #h is an expression that returns the length of h'},
25 {type='text', " String keys don't count towards the length of an array."},
26 {type='text', ' Arrays also stop at the first missing/nil slot. The length of {1, 2, nil, 4} is 2.'},
27 {type='filler', height=15},
28 {type='text', 'Some useful functions operating on tables:', font_size=28},
29 {type='filler', height=15},
30 {type='text', 'pairs(h)', font={size=24}},
31 {type='text',
32 {' Returns an em@{iterator}, a kind of function, that lets you conveniently loop over all the keys of a table.',
33 attrs={
34 em={font={italic=true}}}}},
35 {type='text', ' For example, this program will print out the contents of a table: for key, value in pairs(h) do print(key, value) end'},
36 {type='filler', height=15},
37 {type='text', 'Some useful functions operating on arrays:', font_size=28},
38 {type='filler', height=15},
39 {type='text', 'ipairs(h)', font={size=24}},
40 {type='text', " Returns an iterator that lets you conveniently loop over indexes and elements of an array."},
41 {type='text', ' For example, this program will print out the contents of an array: for _, x in ipairs(h) do print(x) end'},
42 {type='filler', height=10},
43 {type='text', 'table.insert(h, value)', font={size=24}},
44 {type='text', ' Appends a value to the end of array h.'},
45 {type='filler', height=10},
46 {type='text', 'table.insert(h, index, value)', font={size=24}},
47 {type='text', ' Inserts a value at index in the middle of array h.'},
48 {type='filler', height=10},
49 {type='text', 'table.remove(h)', font={size=24}},
50 {type='text', ' Removes the value at the final index of array h.'},
51 {type='filler', height=10},
52 {type='text', 'table.remove(h, index)', font={size=24}},
53 {type='text', ' Removes the value at index in the middle of array h, compacting later values up.'},
54 {type='filler', height=10},
55 {type='text', 'table.sort(h)', font={size=24}},
56 {type='text', ' Sorts the elements in array h.'},
57 {type='filler', height=10},
58 {type='text', 'table.sort(h, f)', font={size=24}},
59 {type='text', ' Sorts the elements in array h by a custom comparison function f.'},
60 {type='text', ' f needs to accept 2 arguments and return true if the first is less than the second.'},
61 {type='filler', height=10},
62 {type='text', 'table.reverse(h)', font={size=24}},
63 {type='text', ' Flips the order of the elements of array h so the final element becomes the first, etc.'},
64 {type='filler', height=10},
65 {type='text', 'table.concat(h)', font={size=24}},
66 {type='text', ' Appends all the elements of array h into a single string and returns it.'},
67 {type='text', ' All the elements must be strings.'},
68 }
69}