Kaotar 5 #1 Posted July 19, 2010 (edited) Bom tava fazendo um algoritmo aqui que precisava dar print em tables, então fiz essa função no rapidão aqui. function printTable(a) b = {} i = 1 s = '{' while true do while (type(a[1]) == "table") do s = s .. '{' ba = a[1] table.remove(a, 1) table.insert(b, 1, a) a = ba end if #a == 0 then if #b > 0 then a = b[1] s = s .. "}, " table.remove(b, 1) else break end end if #a == 1 then s = s .. a[1] else s = s .. a[1] .. ', ' end table.remove(a, 1) end s = s .. "}" print(s) return true end Edited January 23, 2011 by Mickfern Share this post Link to post Share on other sites
Socket 0 #2 Posted July 19, 2010 Tava lendo o code aqui, fico show, com isso lua parece mais com python. Share this post Link to post Share on other sites
Kaotar 5 #3 Posted July 19, 2010 AUHuahauhauahuh A intenção era essa, queria simular Python em Lua, pois tava passando um code de python para Lua... Mas sei la, muito ai é band-aid como diria meu professor, porém tem um recurso bem interessante para quem quer estudar que é o uso de pilha=D, estudando esse code da para criar tabelas bem legais, uma vez precisei disso para um script e não sabia como fazer, então fiz questão de postar para vocês. Att, Kaotar Share this post Link to post Share on other sites
Sinister 0 #4 Posted July 19, 2010 Muito irado essa fucntion '-' Tou tentando entender até agora saokspoaksa Share this post Link to post Share on other sites
Roku 0 #5 Posted July 19, 2010 nao é mais facil unpack? Share this post Link to post Share on other sites
Kaotar 5 #6 Posted July 19, 2010 tenta dar print(unpack({10, 20, {11, 21}}) Share this post Link to post Share on other sites
Dartier 0 #7 Posted July 20, 2010 Os craidores de python poderiam se juntar com os de lua e fazerem um pylua, tá falei merda. BTW ficou bem legal seu script acho que vou adiciona-lo na minha biblioteca =B Share this post Link to post Share on other sites
Socket 0 #8 Posted August 2, 2010 (edited) To fazendo um script aqui, que precisava de uma função como a sua, mas a tabela tinha varias, mas varias tabelas aninhadas, e eu precisava do [], então fiz uma aqui usando recursividade. function printTable(a) local s = "{" for index, value in ipairs(a) do function doStr(str, index, value) if (type(value) == "table") then return str:format(index, printTable(value)) else return str:format(index, value) end end s = s .. doStr("[%s] = %s, ", index, value) end return (s ~= "{") and (s:sub(1, -3) .. "}") or (s .. "}") end Edited January 23, 2011 by Mickfern Share this post Link to post Share on other sites
Kaotar 5 #9 Posted August 6, 2010 Socket isso que você fez não é recursividade, mas ficou legal sua função, porém se tiver uma tabela dentro de outra não irá funcionar. Porém se não for possivel ter uma tabela dentro de outra é sim muito boa, muito melhor que a minha. Share this post Link to post Share on other sites
Mock 32 #10 Posted August 7, 2010 @Kaotar, dava pra roda um for, deletar o valor de uma tabela pode deleta ela em si mesmo sendo no param. @Socket fico legal, magus fez uma chamada var_dump que faz a mesma coisa so que printa com os types, int, string, bool etc Share this post Link to post Share on other sites
Socket 0 #11 Posted August 7, 2010 (edited) @Kaotar, funciona com tabelas aninhadas sim. function printTable(a) local s = "{" for index, value in ipairs(a) do function doStr(str, index, value) if (type(value) == "table") then return str:format(index, printTable(value)) else return str:format(index, value) end end s = s .. doStr("[%s] = %s, ", index, value) end return (s ~= "{") and (s:sub(1, -3) .. "}") or (s .. "}") end print(printTable({2, {7, 6, 2, {8, 65}}})) -- result = {[1] = 2, [2] = {[1] = 7, [2] = 6, [3] = 2, [4] = {[1] = 8, [2] = 65}}} E sim, é recursividade, na 6ª linha da função: return str:format(index, printTable(value)) XD Edited January 23, 2011 by Mickfern Share this post Link to post Share on other sites