This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChaddJackson12's profile picture

Displaying a table?

Started by ChaddJackson12, 28 September 2012 - 10:24 PM
ChaddJackson12 #1
Posted 29 September 2012 - 12:24 AM
I would like to display something like this…


i = fs.list("i")
print(i)

Or something like that. But it is "Attempt to concatenate string and table"… So how could I display a table on a screen?

Also, how could I do something like this with a table… (I am most worried about knowing this one)


i = fs.list("i")
f = fs.open("Somedirectory/" .. i, "w")
input = read
f.writeLine(input)
Cranium #2
Posted 29 September 2012 - 12:36 AM
You can use table.concat(table) to put all of the values together in one mooshed bit, or you can use a separator after your table, like this:

i = fs.list("/")
list = table.concat(i, ", ") --will separate with a comma and space between values
print(list)
The above code will print every value, and separate each value with a comma and a space(", ").
Alternatively, you can use a for loop to accomplish this.

list = fs.list("/")
for i,v in ipairs(list) do  --starting a loop to run for values in list
--i being the iterator, v being the value
  print(v) --print value
end
--OR
list = fs.list("/")
for i = 1,#list do --starts our loop, starting at 1, ending at how many items are in list
   print(list[i]) --will print the indexed value of list on the position indicated by i, the iterator.
end
Both of the above for loops will do the same thing. The syntax is just different.