Print only values, not keys, from table

hi!

I’m building a level creator which lets me quickly design a level and test it. It’s working great!

Levels are stored as tables (grid based game) so I’m trying to use pprint(table) to print the table. But the problem is, each line of pprint shows the K as well as the v.

	127 = 0,

Is there anyway to get pprint to ONLY print the V? Or to remove the Ks from the table? thanks.

You could make your own custom pprint function!
This mostly keeps the general formatting of the pprint output:

function ppprint(tab)
	if type(tab) == "table" then
		local str = "{"..tostring(tab).."\n"
		for key, val in pairs(tab) do
			str = str.."  "..val..",\n"
		end
		str = str.."}"
		print(str)
	else
		print(tab)
	end
end

If you don’t need the old pprint, just do

pprint = ppprint

…and you’re set.

2 Likes

You mean this?

t = {11,12,22,34,46,58}
for i = 1, #t do
    print(t[i])
end

output:

DEBUG:SCRIPT: 11
DEBUG:SCRIPT: 12
DEBUG:SCRIPT: 22
DEBUG:SCRIPT: 34
DEBUG:SCRIPT: 46
DEBUG:SCRIPT: 58
2 Likes

Thanks for your responses!

i need something i can copy and paste which is literally the table with no keys, but i will use an adapted version of Klear’s text. Thanks for that.

… in the end i used this:

			local tab = self.grid
			local str = "{"
			for key, val in pairs(tab) do
				str = str.."  "..val..","
			end
			str = str.."}"
			print(str)

which is great because it means I can literally just copy and paste the string that gets printed into the code. Thanks team!

3 Likes