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

I'm missing something..

Started by brett122798, 06 January 2013 - 08:52 PM
brett122798 #1
Posted 06 January 2013 - 09:52 PM
Okay, I was trying to convert a string into a table and it just will not work! Here's the code:


function filereadline(file, line)
if fs.isDir("/"..file) == false then
if fs.exists("/"..file) == true then
file = fs.open("/"..file, "r")
whole = file:readAll()
file:close()
whole = textutils.unserialize(whole)
if whole[line] == nil then				    -- This is true even if it really isnt
return nil
else
lineOfWhole = whole[line]
return lineOfWhole
end
else
return nil
end
else
return nil
end
end
theoriginalbit #2
Posted 06 January 2013 - 10:09 PM
Ok I'm not seeing any issues here with this code. Is the contents of the file been written in the format that textutils.serialize writes it in?

SLIGHTLY OFF TOPIC:
you have some redundant logic in there. below is the exact same logic as yours, just some refined logic :)/>

function fileReadLine( file, line)
  file = "/"..file
  if fs.exists( file ) and not fs.isDir( file ) then
    local file = fs.open( file, "r" )
    whole = textutils.unserialize( file:readAll() )
    file:close()
    return whole[line]
  end
  return nil
end
brett122798 #3
Posted 06 January 2013 - 10:14 PM
No, I wrote the lines myself in the file, is it any different when you write it in the program?


Yeah, I guessed it could be shortened alot, I kinda made it in a few minutes, so no time to think about it really. :P/>
theoriginalbit #4
Posted 06 January 2013 - 10:31 PM
yeh it is a little different synatx. the whole thing is normally on one line, and all surrounded in { } other than that its pretty much the same as writing it in the program…

example:
{[1]="This",[2]="is",[3]="an",[4]="example",}
brett122798 #5
Posted 06 January 2013 - 10:37 PM
yeh it is a little different synatx. the whole thing is normally on one line, and all surrounded in { } other than that its pretty much the same as writing it in the program…

example:
{[1]="This",[2]="is",[3]="an",[4]="example",}
Ohh.. I'm so stupid.. but lemme ask you this.. if you wanted to convert a regular file's text into a table, how exactly would you do so?
theoriginalbit #6
Posted 06 January 2013 - 10:43 PM
Well it varies… what kind of data is in the file and how does it need to be stored in the table? is the files contents always in a similar format?
brett122798 #7
Posted 06 January 2013 - 10:46 PM
Well it varies… what kind of data is in the file and how does it need to be stored in the table? is the files contents always in a similar format?
Here's what I'm trying to say here. Say there's a regular executable program and I need to read the first line of it for some weirdo reason, how would I read that first line? (Or any line)
theoriginalbit #8
Posted 06 January 2013 - 11:05 PM
you can use readLine, however this can be a bad way sometimes when trying to read a line far down, say if something happens to the program and the file handler stays open. I tend to open the file, read all contents of the file, close the file and then split the string into a table by the new line character "\n" … if you don't know how to write a spilt function I'm sure there are several through the API section…. there is also one in a link in my signature…