Jump to content




Easiest Way To Read Data In A File


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

#1 jay5476

  • Members
  • 289 posts

Posted 23 August 2013 - 03:23 AM

what is the easiest way to insert data from a file after every newline eg.
local table = {}
--[[ file looks like
file
other
test
have a function read that and insert into a table like]]--
table = {"file","other","test"} -- this is what table should be
thanks for help in advanced

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 August 2013 - 03:29 AM

it is quite easy to do this, there are several approaches that people take. This is the approach that I have been taking lately:

--# the lines in the file
local fileData = {}

--# open the file
local handle = assert(fs.open("some_file", 'r'), "Cannot open file for read")

--# read all the lines from the file using an iterator
for line in handle.readLine do
  --# insert the line into the table
  table.insert(fileData, line)
end

--# close the file header
handle.close()

The above is one of the easiest methods.

#3 jay5476

  • Members
  • 289 posts

Posted 23 August 2013 - 03:43 AM

derpy me forgot about readLine bean using readAll lately

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 August 2013 - 04:09 AM

Haha, it's ok, we all have those moments from time to time.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users