Jump to content




Print each string from a table on different lines


  • You cannot reply to this topic
3 replies to this topic

#1 MatazaNz

  • Members
  • 42 posts
  • LocationNew Zealand

Posted 07 July 2013 - 11:24 PM

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.

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 08 July 2013 - 12: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


#3 MatazaNz

  • Members
  • 42 posts
  • LocationNew Zealand

Posted 08 July 2013 - 12:04 AM

View PostKingdaro, on 08 July 2013 - 12:01 AM, said:

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?

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 08 July 2013 - 12:13 AM

You assume correctly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users