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

reading tables from file into multidimensional array

Started by panicmore, 13 June 2014 - 09:27 AM
panicmore #1
Posted 13 June 2014 - 11:27 AM
i have a program where the idea is that I have a file containing tables with components for spells from ars magica 2. So far I have managed to save table to file read it and get the items from my AE network no problem. However I can't work out how to do this with more than one table. I attempted to add the table to a new line but couldn't work out how to read all the different tables into a multidimensional array.
my code is here: http://pastebin.com/c4rHJsFK

the data in the file is:


{[1]=22256,[2]=262,[3]=332,}
{[1]=813,[2]=22256,[3]=22303,}
Edited on 13 June 2014 - 09:29 AM
natedogith1 #2
Posted 13 June 2014 - 11:34 AM
You should be able to put all of the tables you're interested into a single larger table, and then have that table be stored in the file.
Ajt86 #3
Posted 13 June 2014 - 11:37 AM
You could use the following lines of code to read each line of the file then add the table read to a big table (assuming "recipes" is the file in which you saved the tables)

local tables = {}			 -- Stores all the tables
local handle = fs.open("recipes", "r")
while true do
    local text = handle.readLine()
    if text then
	    local recipe = textutils.unserialize(text)
	    if recipe then
		    table.insert(tables, recipe)    -- tables:insert won't work 1.63
	    end
    else
	   break
    end
end
handle.close()
panicmore #4
Posted 13 June 2014 - 11:44 AM
You should be able to put all of the tables you're interested into a single larger table, and then have that table be stored in the file.
I will have over 100 of these recipes so t's easier to have it stored in a text file than in the program

You could use the following lines of code to read each line of the file then add the table read to a big table (assuming "recipes" is the file in which you saved the tables)

local tables = {}			 -- Stores all the tables
local handle = fs.open("recipes", "r")
while true do
	local text = handle.readLine()
	if text then
		local recipe = textutils.unserialize(text)
		if recipe then
			table.insert(tables, recipe)	-- tables:insert won't work 1.63
		end
	else
	   break
	end
end
handle.close()
this was exactly what I wanted thank you !
natedogith1 #5
Posted 13 June 2014 - 11:48 AM
You should be able to put all of the tables you're interested into a single larger table, and then have that table be stored in the file.
I will have over 100 of these recipes so t's easier to have it stored in a text file than in the program
I meant just store the larger table in the external file. It also shouldn't be that difficult if you need to modify the file you already have.
Your file would change into

{{[1]=22256,[2]=262,[3]=332,},
{[1]=813,[2]=22256,[3]=22303,}}
theoriginalbit #6
Posted 13 June 2014 - 11:51 AM
as natedogith1 said, you could easily add all the items to a single table and then serialise that table out to the file. this will make it very easy for you to read back into the program.

to improve on Ajt86's response

local recipes = {}
local h = fs.open("recipes.txt", 'r')
for line in h.readLine do
  table.insert(recipes, line)
end
h.close()

tables:insert won't work 1.63
that won't work in any version of ComputerCraft, you'll get an attempt to call nil.
panicmore #7
Posted 13 June 2014 - 04:03 PM
tables:insert won't work 1.63
that won't work in any version of ComputerCraft, you'll get an attempt to call nil.
it worked perfectly for me
theoriginalbit #8
Posted 13 June 2014 - 04:26 PM
it worked perfectly for me
no it won't, it will look in the table for a function called `insert` which won't exist unless you create it, if it doesn't find a value in the table it looks in the tables metatable not in the table API regardless of the ComputerCraft version… in summary, the only time it will work is in this case

local tbl = {}

function tbl.insert( self, value )
  return table.insert( self, textutils.unserialize( value ) )
end

tbl:insert( "hello" )
Edited by
Xtansia #9
Posted 13 June 2014 - 04:40 PM
to improve on Ajt86's response

local recipes = {}
local h = fs.open("recipes.txt", 'r')
for line in h.readLine do
  table.insert(recipes, line)
end
h.close()

Except you never actually call textutils.unserialize() on the read line
Edited on 13 June 2014 - 02:40 PM
theoriginalbit #10
Posted 13 June 2014 - 05:20 PM
Except you never actually call textutils.unserialize() on the read line
oops, thanks.