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

Print each string from a table on different lines

Started by MatazaNz, 07 July 2013 - 10:24 PM
MatazaNz #1
Posted 08 July 2013 - 12:24 AM
I feel silly now, don't I? How do I print out each string from a table on a new line? For example, I would like to print this table

stuff = {
  "Option1",
  "Option2",
  "Option3"
}

like this in a gui:
Option1
Option2
Option3

I have the gui, I just don't know how to print it out.
Kingdaro #2
Posted 08 July 2013 - 01:01 AM
Loop through and print each one with term.write(). print() and write() would also work semi-well for your purposes, except print() adds a new line, and write() wordwraps text, which could screw up some things in the future. Therefore, it's generally safer using term.write() when making GUIs.

You could make a function that takes an offset, then adds to the specific coordinates of each printed option. Doing "printMenu(5,5)" would print every option at position 5,5.


function printMenu(menu, x, y)
  for i=1, #menu do
    term.setCursorPos(x, y + i - 1)
    term.write(menu[i])
  end
end
MatazaNz #3
Posted 08 July 2013 - 01:04 AM
Loop through and print each one with term.write(). print() and write() would also work semi-well for your purposes, except print() adds a new line, and write() wordwraps text, which could screw up some things in the future. Therefore, it's generally safer using term.write() when making GUIs.

You could make a function that takes an offset, then adds to the specific coordinates of each printed option. Doing "printMenu(5,5)" would print every option at position 5,5.


function printMenu(menu, x, y)
  for i=1, #menu do
	term.setCursorPos(x, y + i - 1)
	term.write(menu[i])
  end
end

I assume menu is where you enter the name of the table?
Kingdaro #4
Posted 08 July 2013 - 01:13 AM
You assume correctly.