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

[Lua][Question]Is it possible to read a table from another file?

Started by Kryptanyte, 06 November 2012 - 06:38 PM
Kryptanyte #1
Posted 06 November 2012 - 07:38 PM
I'm creating a GUI Creator program for my server and am wondering if it is possible to read and write an entire table of strings to put into a GUI Menu without actually going outside the program. Like you are prompted for items to put into the menu, then whatever is input into that program is then written to another file that can be read and turned into the menu itself.

Cheers in advance.
KaoS #2
Posted 07 November 2012 - 02:08 AM
if you want to save the table 'GUI' to the file 'stored' and read it again you would do the following

SAVE

local oFile=io.open('stored','w')
oFile:write(textutils.serialize(GUI))
oFile:close()

LOAD

local oFile=io.open('stored','r')
GUI=textutils.unserialize(oFile:read())
oFile:close()
Kryptanyte #3
Posted 07 November 2012 - 05:27 PM
What if I'm trying to use an input/multiple inputs to save to a table? That would require a function to split at each line break and save how many there are then order each of the inputs into a table equal to the amount of line breaks wouldn't it?

I'm probably over complicating this thing as this is probably the first time I've ever tried to use programs to read/write files.

Also KaoS, not sure if its on purpose or not but if the save code is not given GUI variable it will write the last known one to the file, even if the 'stored' file is deleted.
KaoS #4
Posted 08 November 2012 - 01:50 AM
GUI is a table of values, we do not have to split it into lines or anything, that is all taken care of already in the textutils.serialize function

it will overwrite or create the file 'stored' when you save it. you will have to make sure that GUI is defined though or you will get errors. just use

GUI=GUI or --default value--
to set a fallback if GUI is not defined
Kryptanyte #5
Posted 08 November 2012 - 10:52 AM
That half answers my question, but the major piece of what I'm trying to figure out is, how can I write a table based on inputs from the program that is completely undefined in the code. So there is only something that will define that various inputs will equal 'GUI' and be saved in table format. So something like this;



while true do

  if input == exit() then

    break

  else

    (store this string in a table)

  end

end

(write stored table to file)


Just tell me if you don't quite understand. It might be a little badly worded.
Kingdaro #6
Posted 08 November 2012 - 10:58 AM
You use read() to take user input. read() halts the program and waits for the user to type something, then press enter.


local inputTable = {}

while true do
	local input = read() -- we will say the user types "foo"
	if input == 'exit()' then
		break
	else
		table.insert(inputTable, input) -- put "foo" into the inputTable
	end
end

local serialized = textutils.serialize(inputTable) -- turn the inputTable into a string for writing

local file = fs.open('inputs', 'w') -- open the file called "inputs" for writing
if file then -- check if we opened it successfully
	file.write(serialized) -- write the table string
	file.close() -- close the file
end
Kryptanyte #7
Posted 08 November 2012 - 11:26 AM
Ahh thank you. That is what I wanted. Thank you very much.