{ {type='rows', default={font={size=21}, line_height=25}, {type='text', font={size=28}, line_height=50, {'Lua@{Lua} > tables', attrs={ Lua={target='--lua'}, }}, }, {type='filler', height=15}, {type='text', "Lua's tables are a versatile way to group data together in ways that make it easy to slice and dice."}, {type='filler', height=15}, {type='text', '{} is an empty table value/expression.'}, {type='filler', height=10}, {type='text', {'h = {11, 12, 13} creates an em@{array}, a table that can be looked up by number em@{keys}.', attrs={ em={font={italic=true}}}}}, {type='text', ' h[1] is now 11, h[2] is 12 and so on.'}, {type='filler', height=10}, {type='text', 'h = {a=34} creates a table that can be looked up by string keys.'}, {type='text', " h['a'] is now 34, h['b'] is 35."}, {type='text', ' You can also shorten constant string keys using periods. h.a is 34, h.b is 35.'}, {type='filler', height=10}, {type='text', 'if h is an array. #h is an expression that returns the length of h'}, {type='text', " String keys don't count towards the length of an array."}, {type='text', ' Arrays also stop at the first missing/nil slot. The length of {1, 2, nil, 4} is 2.'}, {type='filler', height=15}, {type='text', 'Some useful functions operating on tables:', font_size=28}, {type='filler', height=15}, {type='text', 'pairs(h)', font={size=24}}, {type='text', {' Returns an em@{iterator}, a kind of function, that lets you conveniently loop over all the keys of a table.', attrs={ em={font={italic=true}}}}}, {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'}, {type='filler', height=15}, {type='text', 'Some useful functions operating on arrays:', font_size=28}, {type='filler', height=15}, {type='text', 'ipairs(h)', font={size=24}}, {type='text', " Returns an iterator that lets you conveniently loop over indexes and elements of an array."}, {type='text', ' For example, this program will print out the contents of an array: for _, x in ipairs(h) do print(x) end'}, {type='filler', height=10}, {type='text', 'table.insert(h, value)', font={size=24}}, {type='text', ' Appends a value to the end of array h.'}, {type='filler', height=10}, {type='text', 'table.insert(h, index, value)', font={size=24}}, {type='text', ' Inserts a value at index in the middle of array h.'}, {type='filler', height=10}, {type='text', 'table.remove(h)', font={size=24}}, {type='text', ' Removes the value at the final index of array h.'}, {type='filler', height=10}, {type='text', 'table.remove(h, index)', font={size=24}}, {type='text', ' Removes the value at index in the middle of array h, compacting later values up.'}, {type='filler', height=10}, {type='text', 'table.sort(h)', font={size=24}}, {type='text', ' Sorts the elements in array h.'}, {type='filler', height=10}, {type='text', 'table.sort(h, f)', font={size=24}}, {type='text', ' Sorts the elements in array h by a custom comparison function f.'}, {type='text', ' f needs to accept 2 arguments and return true if the first is less than the second.'}, {type='filler', height=10}, {type='text', 'table.reverse(h)', font={size=24}}, {type='text', ' Flips the order of the elements of array h so the final element becomes the first, etc.'}, {type='filler', height=10}, {type='text', 'table.concat(h)', font={size=24}}, {type='text', ' Appends all the elements of array h into a single string and returns it.'}, {type='text', ' All the elements must be strings.'}, } }