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

[LUA] [Help] Getting Data From Files

Started by robertcarr22, 01 May 2013 - 09:01 AM
robertcarr22 #1
Posted 01 May 2013 - 11:01 AM
Here is the code In full: http://pastebin.com/nzkjcdwt

I am writing a todo list and message board and I have the todo list functionality fully working including saving and loading data from files.

The problem I'm having is in loading and saving data for the message board.
I want to save both the username and the message in a table within a table.
e.g Table[1] would have another table in it storing more information.

I use this technique in my code for the GUI but I'm struggling with this part of the code (lines 82-102):

  local file = fs.open("mes","r")
  local i = 1
  local line = file.readLine()

  for username, mess in string.gmatch(line, "(%w+):(/>/>%w+)") do
	mesData[i].user = username
	mesData[i].message = mess
	print(i)
	line = file.readLine()
	if line == nil then
	  break
	else
	  i = i + 1
	end	  
  end

  file.close()


If you cant tell from the code the separate file will be set up like this "username:message"
So when it comes to reading the file i want it to separate the string and save them as two separate variables in a table within a table.

Any help or advice on my coding will be greatly appreciated! :)/>
theoriginalbit #2
Posted 02 May 2013 - 01:20 AM
this is how I would do it

-- #open the file
local handle = fs.open('mes', 'r')
-- #create our table
local mesData = {}
-- #loop through the file
for line in handle.readLine do
  -- #find the first occurrence of :
  local location = (line:find(':', 1))
  if location then -- #if we did find a :
	-- #get the user, which is everything before the location we found
	local usr = line:sub(1, location - 1)
	-- #get the message, which is everything after the location we found
	local msg = line:sub( location + 1 )
	-- #insert our sub table into the main table
	table.insert(mesData, {user = usr, message = msg})
  else -- #print that there was no : on this line
	print('No : found on this line')
  end
end
-- #close the file
handle.close()

Now if you wanted to add support for multiline messages you could do the following

-- #open the file
local handle = fs.open('mes', 'r')
-- #create our table
local mesData = {}
-- #loop through the file
for line in handle.readLine do
  -- #find the first occurrence of :
  local location = (line:find(':', 1))
  if location then -- #if we did find a :
	-- #get the user, which is everything before the location we found
	local usr = line:sub(1, location - 1)
	-- #get the message, which is everything after the location we found
	local msg = line:sub( location + 1 )
	-- #insert our sub table into the main table
	table.insert(mesData, {user = usr, message = msg})
  elseif line then -- #if we did not find a ':' but there was something on the line, we assume this message is part of previous, so we add it
	mesData[#mesData].message = mesData[#mesData].message..'\n'..line
  end
end
-- #close the file
handle.close()
robertcarr22 #3
Posted 02 May 2013 - 09:52 AM
this is how I would do it
-snip-

This looks good! will try it out later tonight! :)/>