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

save table , fast load table...

Started by etopsirhc, 13 September 2012 - 10:16 PM
etopsirhc #1
Posted 14 September 2012 - 12:16 AM
currently i have a program that will read a builders mod text file to a 3d table but any time i log out i have to re-read that entire file into the table. and being the size it is … it will probably take a day rl time

so that said , is there an easy way to write a table to file so all i have to do to load it back up is

file = io.open("file","r")
table = file:read("*all")
file:close()

and for those wondering , its a 6.6MB text file containing 120 layers , each layer is a set of 257 lines , each 113 used charicters long ( space delimited )

so yeah , huge file faster read time would be much appricated
MysticT #2
Posted 14 September 2012 - 12:21 AM
6.6MB? What are you storing there?
You can read the whole file using file.readAll(), but it will return it as a string, not a table.
etopsirhc #3
Posted 14 September 2012 - 12:25 AM
yeah , i'm actualy using line by line cause some lines have only semi useful info on them

https://dl.dropbox.com/u/47442169/ragnarok
^
__ thats what's stored
MysticT #4
Posted 14 September 2012 - 12:37 AM
Well, if all the layers have the same size, you can store it in the first line/s and then read that many lines:

local layers = tonumber(file.readLine()) -- first line contains the number of layers
local size = tonumber(file.readLine()) -- second line contains number of lines per layer
local t = {}
for i = 1, layers do
  local layer = {}
  for j = 1, size do
    local sLine = file.readLine()
    local line = {}
    for n in string.gmatch(sLine, "%d+") do
	  table.insert(line, tonumber(n))
    end
    table.insert(layer, line)
  end
  table.insert(t, layer)
end
That should make it a little faster.
etopsirhc #5
Posted 14 September 2012 - 12:40 AM
ok , i'll have to see how that works out , be back after i implement it