Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Print table contents onto 1 line?

Asked by
iFlusters 355 Moderation Voter
8 years ago
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

Log in to vote
1
Answered by
theCJarmy7 1293 Moderation Voter
8 years ago

table.concat!

print(table.concat(myTable," this is inbetween the things"))

for more information, check the wiki page for table.concat

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

Answer this question