{ {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', "As tabelas do Lua são uma forma versátil de agrupar dados de maneiras que facilitam fatiar e cortar."}, {type='filler', height=15}, {type='text', '{} é um valor/expressão de tabela vazia.'}, {type='filler', height=10}, {type='text', {'h = {11, 12, 13} cria um em@{array}, uma tabela que pode ser consultada por em@{chaves} numéricas.', attrs={ em={font={italic=true}}}}}, {type='text', ' h[1] agora é 11, h[2] é 12 e assim por diante.'}, {type='filler', height=10}, {type='text', 'h = {a=34} cria uma tabela que pode ser consultada por chaves string.'}, {type='text', " h['a'] agora é 34, h['b'] é 35."}, {type='text', ' Você também pode abreviar chaves string constantes usando pontos. h.a é 34, h.b é 35.'}, {type='filler', height=10}, {type='text', 'se h for um array. #h é uma expressão que retorna o comprimento de h'}, {type='text', " Chaves string não contam para o comprimento de um array."}, {type='text', ' Arrays também param no primeiro slot ausente/nil. O comprimento de {1, 2, nil, 4} é 2.'}, {type='filler', height=15}, {type='text', 'Algumas funções úteis operando em tabelas:', font_size=28}, {type='filler', height=15}, {type='text', 'pairs(h)', font={size=24}}, {type='text', {' Retorna um em@{iterador}, um tipo de função, que permite convenientemente percorrer todas as chaves de uma tabela.', attrs={ em={font={italic=true}}}}}, {type='text', ' Por exemplo, este programa vai imprimir o conteúdo de uma tabela: for key, value in pairs(h) do print(key, value) end'}, {type='filler', height=15}, {type='text', 'Algumas funções úteis operando em arrays:', font_size=28}, {type='filler', height=15}, {type='text', 'ipairs(h)', font={size=24}}, {type='text', " Retorna um iterador que permite convenientemente percorrer índices e elementos de um array."}, {type='text', ' Por exemplo, este programa vai imprimir o conteúdo de um 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', ' Adiciona um valor ao final do array h.'}, {type='filler', height=10}, {type='text', 'table.insert(h, index, value)', font={size=24}}, {type='text', ' Insere um valor no índice no meio do array h.'}, {type='filler', height=10}, {type='text', 'table.remove(h)', font={size=24}}, {type='text', ' Remove o valor no índice final do array h.'}, {type='filler', height=10}, {type='text', 'table.remove(h, index)', font={size=24}}, {type='text', ' Remove o valor no índice no meio do array h, compactando valores posteriores para cima.'}, {type='filler', height=10}, {type='text', 'table.sort(h)', font={size=24}}, {type='text', ' Ordena os elementos no array h.'}, {type='filler', height=10}, {type='text', 'table.sort(h, f)', font={size=24}}, {type='text', ' Ordena os elementos no array h por uma função de comparação personalizada f.'}, {type='text', ' f precisa aceitar 2 argumentos e retornar true se o primeiro for menor que o segundo.'}, {type='filler', height=10}, {type='text', 'table.reverse(h)', font={size=24}}, {type='text', ' Inverte a ordem dos elementos do array h para que o elemento final se torne o primeiro, etc.'}, {type='filler', height=10}, {type='text', 'table.concat(h)', font={size=24}}, {type='text', ' Concatena todos os elementos do array h em uma única string e a retorna.'}, {type='text', ' Todos os elementos devem ser strings.'}, } }