Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Print table contents onto 1 line?

local MyTable = {"Foo", "Fee"}

print(MyTable[1], MyTable[2])

I can do that fine, but what if the script doesn't know how many there are in the table? How do I make it so it prints the entire table onto one line rather than:

Foo Fee

I would have

Foo Fee

I'm guessing it would require some sort of loops so all I've come up with is this:

for x, Table in pairs(MyTable) do
	print(Table) --But how do I keep them all on one line?
end

2 answers

The CompileTable is the solution! It will print as {"Foo","Fee"}

function compileTable(table)
   local index = 1
   local holder = "{"
   while true do
      if type(table[index]) == "function" then
         index = index + 1
      elseif type(table[index]) == "table" then
         holder = holder..compileTable(table[index])
      elseif type(table[index]) == "number" then
         holder = holder..tostring(table[index])
      elseif type(table[index]) == "string" then
         holder = holder.."\""..table[index].."\""
      elseif table[index] == nil then
         holder = holder.."nil"
      elseif type(table[index]) == "boolean" then
         holder = (table[index] and "true" or "false")
      end
      if index + 1 > #table then
        break
      end
      holder = holder..","
      index = index + 1
   end
   return holder.."}"
end

local compiledTable = compileTable(MyTable)
print(compiledTable)

This discussion is preserved as it was. New answers, comments, and votes are closed.