Jump to content




I'm missing something..


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

#1 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

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


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


#3 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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",}

#5 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 06 January 2013 - 10:37 PM

View PostTheOriginalBIT (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",}
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?

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 06 January 2013 - 10:46 PM

View PostTheOriginalBIT (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?
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)

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users