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

file.readline(number) ?

Started by etopsirhc, 17 December 2012 - 03:22 PM
etopsirhc #1
Posted 17 December 2012 - 04:22 PM
is there an easy way to read a file at X number ? or do i have to loop to that line to read it .
note: i do not want to read the entire file at once as its huge ( MBs not KBs )
theoriginalbit #2
Posted 17 December 2012 - 04:27 PM
In lua there is a file seek option in the io. unfortunately this doesn't exist in CC

Reference:
http://computercraft.info/wiki/IO_(API)


EDIT: Upon further investigation the file seek wouldn't have done what you wanted to.
etopsirhc #3
Posted 17 December 2012 - 04:33 PM
yeah, lol the nice "doesn't exist" next to it seems to make me believe it wouldn't have worked very well lol
theoriginalbit #4
Posted 17 December 2012 - 04:36 PM
yeah, lol the nice "doesn't exist" next to it seems to make me believe it wouldn't have worked very well lol

ok here is one solution that I just thought of. At startup load the file into a table of the lines. then in the code to read from say like 50 to 100 you would do this


for = 50, 100 do
  line = tableName[i]
  -- do something with the line
end
etopsirhc #5
Posted 17 December 2012 - 04:45 PM
trying not to load the entire file at once , just line by line as needed , as its rather big for a CC file (almost 84k lines )
basically this is for my printer presistance , cause if say the server crashes and i have to restart the program i want it to pick up from the line it left off on.
theoriginalbit #6
Posted 17 December 2012 - 04:47 PM
trying not to load the entire file at once , just line by line as needed , as its rather big for a CC file (almost 84k lines )
basically this is for my printer presistance , cause if say the server crashes and i have to restart the program i want it to pick up from the line it left off on.

It makes sense why you don't want to load it all, but unless one of the other guys knows a way then loading it all at startup may be the only way to do it.
etopsirhc #7
Posted 17 December 2012 - 04:51 PM
ok , guess for now i'll loop though it as needed
theoriginalbit #8
Posted 17 December 2012 - 04:56 PM
loop through a loaded table. it will be quicker than opening and looping through the file each time.
etopsirhc #9
Posted 17 December 2012 - 05:23 PM
the looping through it to a certain line will only happen when the server stops , as i plan to chunk load the area around the turtle
Dlcruz129 #10
Posted 17 December 2012 - 05:46 PM
How the hell do you even make a program that big? Anyways, you have to read the whole file, but you can put the lines into a table, extract the line, then delete the table like this, after the lines are in the table:

stuff = fileData[80]
fileData = nil
print(stuff)
etopsirhc #11
Posted 17 December 2012 - 08:20 PM
the program isnt that big , the file containing all the coordinates of each block that needs to be placed in a HUGE model is
theoriginalbit #12
Posted 17 December 2012 - 08:35 PM
ahhh its like that. I will admit I haven't done it in Lua but i have read much larger files in Java. i read into an Object a file that had 200k points that all had to be parsed and converted. Took ~100milliseconds to load the file.
JoshhT #13
Posted 17 December 2012 - 08:55 PM
I made this a while ago. Perhaps you should look through it.
http://pastebin.com/ui0xdsLy
etopsirhc #14
Posted 17 December 2012 - 09:28 PM
ahhh its like that. I will admit I haven't done it in Lua but i have read much larger files in Java. i read into an Object a file that had 200k points that all had to be parsed and converted. Took ~100milliseconds to load the file.

the first time i tried making one i decided to load phrased info into a 3d table, it loaded a line of text and searched though the line for 1's to get the block coords
it took over 5 hrs to finish reading the file, ><

i cut the file down a ton by doing that heavy part in java and writing out relative coords. so now i just load line, phrase it to 3 number and tell the turtle what to do , no extra pre-loading.
theoriginalbit #15
Posted 17 December 2012 - 09:43 PM
ahhh its like that. I will admit I haven't done it in Lua but i have read much larger files in Java. i read into an Object a file that had 200k points that all had to be parsed and converted. Took ~100milliseconds to load the file.

the first time i tried making one i decided to load phrased info into a 3d table, it loaded a line of text and searched though the line for 1's to get the block coords
it took over 5 hrs to finish reading the file, ><

i cut the file down a ton by doing that heavy part in java and writing out relative coords. so now i just load line, phrase it to 3 number and tell the turtle what to do , no extra pre-loading.

I also had to write a program in Java this semester at university, it had to calculate fan-in and fan-out data of classes. The requirement was that it had to read ~20,000 class files and calculate the data and then write out that data to a file, all under 10 seconds. I was helping people who were having issues with getting it under 10 seconds, some people were getting upwards of 5 minutes. Mine ran in 120milliseconds. 2nd fastest in the unit, guy that was better multithreaded and was writing while calculating, or something.
JoshhT #16
Posted 17 December 2012 - 11:09 PM
guy that was better multithreaded and was writing while calculating, or something.
Threads in Java are amazing, and very powerful. The only downside to them is how resource intensive they are.
I love Java.
theoriginalbit #17
Posted 17 December 2012 - 11:14 PM
yeh but he only had ~18ms speed compared to mine. wasn't much of an advantage