Jump to content
Sign in to follow this  
Kaotar

Função printTable

Recommended Posts

Kaotar    5
Kaotar

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 by Mickfern

Share this post


Link to post
Share on other sites
Socket    0
Socket

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
Kaotar

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
Sinister

Muito irado essa fucntion '-'

Tou tentando entender até agora saokspoaksa

Share this post


Link to post
Share on other sites
Roku    0
Roku

nao é mais facil unpack?

Share this post


Link to post
Share on other sites
Kaotar    5
Kaotar

tenta dar print(unpack({10, 20, {11, 21}})

Share this post


Link to post
Share on other sites
Dartier    0
Dartier

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
Socket

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 by Mickfern

Share this post


Link to post
Share on other sites
Kaotar    5
Kaotar

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
Mock

@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
Socket

@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 by Mickfern

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×