Jump to content




file.readline(number) ?


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

#1 etopsirhc

  • Members
  • 122 posts

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 )

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#3 etopsirhc

  • Members
  • 122 posts

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 04:36 PM

View Postetopsirhc, on 17 December 2012 - 04:33 PM, said:

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


#5 etopsirhc

  • Members
  • 122 posts

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.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 04:47 PM

View Postetopsirhc, on 17 December 2012 - 04:45 PM, said:

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.

#7 etopsirhc

  • Members
  • 122 posts

Posted 17 December 2012 - 04:51 PM

ok , guess for now i'll loop though it as needed

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#9 etopsirhc

  • Members
  • 122 posts

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

#10 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

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)

#11 etopsirhc

  • Members
  • 122 posts

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

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#13 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 17 December 2012 - 08:55 PM

I made this a while ago. Perhaps you should look through it.
http://pastebin.com/ui0xdsLy

#14 etopsirhc

  • Members
  • 122 posts

Posted 17 December 2012 - 09:28 PM

View PostTheOriginalBIT, on 17 December 2012 - 08:35 PM, said:

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.

#15 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 09:43 PM

View Postetopsirhc, on 17 December 2012 - 09:28 PM, said:

View PostTheOriginalBIT, on 17 December 2012 - 08:35 PM, said:

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.

#16 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 17 December 2012 - 11:09 PM

View PostTheOriginalBIT, on 17 December 2012 - 08:35 PM, said:

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.

#17 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 11:14 PM

yeh but he only had ~18ms speed compared to mine. wasn't much of an advantage





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users