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
I'm missing something..
Started by brett122798, Jan 06 2013 09:52 PM
7 replies to this topic
#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:
#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
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
#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.
Yeah, I guessed it could be shortened alot, I kinda made it in a few minutes, so no time to think about it really.
#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",}
example:
{[1]="This",[2]="is",[3]="an",[4]="example",}
#5
Posted 06 January 2013 - 10:37 PM
TheOriginalBIT (OnHoliday), on 06 January 2013 - 10:31 PM, said:
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",}
example:
{[1]="This",[2]="is",[3]="an",[4]="example",}
#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?
#7
Posted 06 January 2013 - 10:46 PM
TheOriginalBIT (OnHoliday), on 06 January 2013 - 10:43 PM, said:
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?
#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...
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











