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

Retrieve table from file

Started by Cranium, 12 August 2012 - 01:33 AM
Cranium #1
Posted 12 August 2012 - 03:33 AM
I have been trying to retrieve the table that I created from a file, but it does not get retrieved in any format that I know how to use. I had it successfully save, and I believe I can retrieve it, but to convert that to a usable table is beyond me. Here is the table that I created in the previous program:

Admin,craniumkid22,xxxxx
User,miveck,yyyyy
Lights,0,false
7,false,false
Reactor,16384,true
Coolant,2048,true
4,1,nil
Macerator,16,false
Furnace,2,false
Compressor,8,false
Extractor,8192,false
The rows correspond to the correct items, but using my load function, I'm just not sure how to apply the results to the rest of my code. This is my loading code:

local function loadState()
    local loadTable = {}
    local file = fs.open("status", "r")
    if file then
	    readLine = file.readLine()
	    while readLine do
		    local lineTable = {}
		    lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%a+),(%a+),(%a+)")
		    if lineTable.type then
			    table.insert(loadTable, lineTable)
		    end
		    readLine = file.readLine()
	    end
	    file.close()
    end
    return loadTable
end
I thought I could simply do this:

table1 = loadState()
print(table1[1])
and it would print the first line, but I have no idea how… I am unable to use these lines without knowing how…
Lyqyd #2
Posted 12 August 2012 - 05:47 AM
It loads up a table of tables, so you could do:


table1 = loadState()
print(table[1].type..","..table[1].color..","..table[1].status)

Or to loop through all of them:


table = loadState()
--dNum is the index in table of each sub-table, dInfo is each subtable.
for dNum, dInfo in ipairs(table) do
  if dInfo.type == "lights" then
    print("Lights are using color value "..dInfo.color)
  end
end
Cranium #3
Posted 12 August 2012 - 06:13 AM
So how would I call upon those variables to use on another function. Say I want to call on the status of lights, then if on, turn them off. (or vice versa) Of course then I would just use my saveState() function to save the new state.
Lyqyd #4
Posted 12 August 2012 - 06:23 AM

controlTable = loadState()
for dNum, dInfo in ipairs(controlTable) do
	if dInfo.type == "lights" then
		if dInfo.status == "true" then
			local output = rs.getBundledOutput("side")
			rs.setBundledOutput("side", colors.subtract(output, dInfo.color))
			dInfo.status = "false"
		end
		break
	end
end
saveState(controlTable)
Cranium #5
Posted 12 August 2012 - 06:25 AM
Wow, thank you. You are so helpful, I don't know what I'd do without you.
Cranium #6
Posted 12 August 2012 - 04:01 PM
Well, I'm still having trouble working with this. I tried the code given here, but it doesn't seem to recognize the proper variables from the table. I threw together a quick test program to test the load, but it will never print that the lights are true, even thought in the status file they are. Here's my kludged-together program to test it:

local function loadState()
    local loadTable = {}
    local file = fs.open("status", "r")
    if file then
	    readLine = file.readLine()
	    while readLine do
		    local lineTable = {}
		    lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%a+),(%a+),(%a+)")
		    if lineTable.type then
			    table.insert(loadTable, lineTable)
		    end
		    readLine = file.readLine()
	    end
	    file.close()
    end
    return loadTable
end
controlTable = loadState()
for dNum, dInfo in ipairs(controlTable) do
if dInfo.type == "Lights" then
print("lights true")
  if dInfo.status == "true" then
  local output = rs.getBundledOutput("back")
  print("lights off")
  sleep(5)
  rs.setBundledOutput("side", colors.subtract(output, dInfo.color))
  dInfo.status = "false"
  else
  local output = rs.getBundledOutput("back")
  print("lights on")
  sleep(5)
  rs.setBundledOutput("side", colors.combine(output, dInfo.color))
  dInfo.status = "true"
  end break
end
end
It should print "lights true" if there is a status for lights. I used the same file as before, so there should be no problem… At least none that I could see…
Cranium #7
Posted 12 August 2012 - 05:16 PM
So I found that it doesn't like numbers being entered into the file. How do I prevent that from being ignored? Right now the code I have will just print a portion of my file. It only ignores the lines with numbers in it. How do I tell it to print that?

local function loadState()
    local loadTable = {}
    local file = fs.open("status", "r")
    if file then
	    readLine = file.readLine()
	    while readLine do
		    local lineTable = {}
		    lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%a+),(%a+),(%a+)")
		    if lineTable.type then
			    table.insert(loadTable, lineTable)
		    end
		    readLine = file.readLine()
	    end
	    file.close()
    end
    return loadTable
end
controlTable = loadState()
for dNum, dInfo in ipairs(controlTable) do
print(dInfo.type,": ",dInfo.color,", ",dInfo.status)
end
Lyqyd #8
Posted 12 August 2012 - 05:35 PM
The string.match line is the issue. Change each '%a+' to '%w+' to match all alphanumerics rather than just all alphabetical characters.
Cranium #9
Posted 12 August 2012 - 05:47 PM
Thanks, that worked! Now I can actually use those values.