Jump to content




reading tables from file into multidimensional array


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

#1 panicmore

  • Members
  • 26 posts
  • LocationEssex, UK

Posted 13 June 2014 - 10: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 by panicmore, 13 June 2014 - 10:29 AM.


#2 natedogith1

  • Members
  • 110 posts

Posted 13 June 2014 - 10: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.

#3 Ajt86

  • Members
  • 42 posts

Posted 13 June 2014 - 10: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()


#4 panicmore

  • Members
  • 26 posts
  • LocationEssex, UK

Posted 13 June 2014 - 10:44 AM

View Postnatedogith1, on 13 June 2014 - 10:34 AM, said:

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

View PostAjt86, on 13 June 2014 - 10:37 AM, said:

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 !

#5 natedogith1

  • Members
  • 110 posts

Posted 13 June 2014 - 10:48 AM

View Postpanicmore, on 13 June 2014 - 10:44 AM, said:

View Postnatedogith1, on 13 June 2014 - 10:34 AM, said:

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,}}


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 June 2014 - 10: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()

View PostAjt86, on 13 June 2014 - 10:37 AM, said:

tables:insert won't work 1.63
that won't work in any version of ComputerCraft, you'll get an attempt to call nil.

#7 panicmore

  • Members
  • 26 posts
  • LocationEssex, UK

Posted 13 June 2014 - 03:03 PM

View Posttheoriginalbit, on 13 June 2014 - 10:51 AM, said:


View PostAjt86, on 13 June 2014 - 10:37 AM, said:

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

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 June 2014 - 03:26 PM

View Postpanicmore, on 13 June 2014 - 03:03 PM, said:

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 theoriginalbit, 13 June 2014 - 04:20 PM.
added textutils.unserialize


#9 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 13 June 2014 - 03:40 PM

View Posttheoriginalbit, on 13 June 2014 - 10:51 AM, said:

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 by tomass1996, 13 June 2014 - 03:40 PM.


#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 June 2014 - 04:20 PM

View Posttomass1996, on 13 June 2014 - 03:40 PM, said:

Except you never actually call textutils.unserialize() on the read line
oops, thanks.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users