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

Table Data is being set to nil when written to screen

Started by RAWK-HIGH, 18 May 2014 - 04:36 AM
RAWK-HIGH #1
Posted 18 May 2014 - 06:36 AM
I'm having a problem with the Mall Directory i'm making for a server i'm an admin on. it "worked" until i added the code at the top to save the data in a file. There is another computer that allows admins to edit the shop name on the screen witch was working again till i added the file function to my code. Any help would be greatly appreciated. here is a picture of the directory.(http://prntscr.com/3k8t2w) This code is only for one screen i have not yet change the others to this set up they only get there info when entered from main comp and lose there info if restart.


monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(14,1)
monitor.setTextColor(colors.red)
monitor.write("First Floor")
monitor.setTextColor(colors.lightBlue)
rednet.open ("left")



function check()
  if not fs.exists("shopFloor") then
    local file = fs.open("shopFloor", "w")
    file.write()
    file.close()
  end
end

function readf()
  local file = fs.open("shopFloor", "r")
  shopTable = file.readAll()
  file.close()
  return textutils.unserialize(shopTable)
end

function savef()
  local file = fs.open("shopFloor", "w")
  textutils.serialize(shopTable)
  file.write(shopTable)
  file.close()
end





local shopTable = ""
local file = ""
local event = ""
local shopName1 = shopTable[1]
local shopName2 = shopTable[2]
local shopName3 = shopTable[3]
local shopName4 = shopTable[4]
local shopName5 = shopTable[5]
local shopName6 = shopTable[6]
local shopName7 = shopTable[7]
local shopName8 = shopTable[8]
local shopName9 = shopTable[9]
local shopName10 = shopTable[10]
local shopName11 = shopTable[11]
local shopName12 = shopTable[12]

check()
while true do
readf()

monitor.setCursorPos(2, 2)
monitor.clearLine()
monitor.write("101 - "..tostring(shopName1))
monitor.setCursorPos(2, 3)
monitor.clearLine()
monitor.write("102 - "..tostring(shopName2))
monitor.setCursorPos(2, 4)
monitor.clearLine()
monitor.write("103 - "..tostring(shopName3))
monitor.setCursorPos(2, 5)
monitor.clearLine()
monitor.write("104 - "..tostring(shopName4))
monitor.setCursorPos(2, 6)
monitor.clearLine()
monitor.write("105 - "..tostring(shopName5))
monitor.setCursorPos(2, 7)
monitor.clearLine()
monitor.write("106 - "..tostring(shopName6))
monitor.setCursorPos(2, 8)
monitor.clearLine()
monitor.write("107 - "..tostring(shopName7))
monitor.setCursorPos(2, 9)
monitor.clearLine()
monitor.write("108 - "..tostring(shopName8))
monitor.setCursorPos(2, 10)
monitor.clearLine()
monitor.write("109 - "..tostring(shopName9))
monitor.setCursorPos(2, 11)
monitor.clearLine()
monitor.write("110 - "..tostring(shopName10))
monitor.setCursorPos(2, 12)
monitor.clearLine()
monitor.write("111 - "..tostring(shopName11))
monitor.setCursorPos(2, 13)
monitor.clearLine()
monitor.write("112 - "..tostring(shopname12))

id, msg = rednet.receive()
table = textutils.unserialize(msg)
id = table[1]
text = table[2]
event = os.pullEvent()
if event == "rednet_message" then
    if id == 101 then
	  shopName1 = text
	  monitor.setCursorPos(2, 2)
	  monitor.clearLine()
	  monitor.write("101 - " .. shopName1)
    elseif id == 102 then
	  shopName2 = text
	  monitor.setCursorPos(2, 3)
	  monitor.clearLine()
	  monitor.write("102 - " .. shopName2)
    elseif id == 103 then
	  shopName3 = text
	  monitor.setCursorPos(2, 4)
	  monitor.clearLine()
	  monitor.write("103 - " .. shopName3)
    elseif id == 104 then
	  shopName4 = text
	  monitor.setCursorPos(2, 5)
	  monitor.clearLine()
	  monitor.write("104 - " .. shopName4)
    elseif id == 105 then
	  shopName5 = text
	  monitor.setCursorPos(2, 6)
	  monitor.clearLine()
	  monitor.write("105 - " .. shopName5)
    elseif id == 106 then
	  shopName6 = text
	  monitor.setCursorPos(2, 7)
	  monitor.clearLine()
	  monitor.write("106 - " .. shopName6)
    elseif id == 107 then
	  shopName7 = text
	  monitor.setCursorPos(2, 8)
	  monitor.clearLine()
	  monitor.write("107 - " .. shopName7)
    elseif id == 108 then
	  shopName8 = text
	  monitor.setCursorPos(2, 9)
	  monitor.clearLine()
	  monitor.write("108 - " .. shopName8)
    elseif id == 109 then
	  shopName9 = text
	  monitor.setCursorPos(2, 10)
	  monitor.clearLine()
	  monitor.write("109 - " .. shopName9)
    elseif id == 110 then
	  shopName10 = text
	  monitor.setCursorPos(2, 11)
	  monitor.clearLine()
	  monitor.write("110 - " .. shopName10)
    elseif id == 111 then
	  shopName11 = text
	  monitor.setCursorPos(2, 12)
	  monitor.clearLine()
	  monitor.write("111 - " .. shopName11)
    elseif id == 112 then
	  shopName12 = text
	  monitor.setCursorPos(2, 13)
	  monitor.clearLine()
	  monitor.write("112 - " .. shopName12)
  end
end
savef()
end
RoD #2
Posted 18 May 2014 - 01:06 PM
First:
I dont get why your "check" function needs to read a file and writes nothing… if just want to see if it exists you can just use:

if fs.exists("filename")
Anyways change:

function savef()
  local file = fs.open("shopFloor", "w")
  textutils.serialize(shopTable)
  file.write(shopTable)
  file.close()
end
To:

function savef()
  local file = fs.open("shopFloor", "w")
  variable = textutils.serialize(shopTable)
  file.write(variable)--#You need to return the serialized table in order to write it down to a file
  file.close()
end
I think that this should work.
Edited on 18 May 2014 - 11:06 AM
RAWK-HIGH #3
Posted 18 May 2014 - 08:53 PM
Thank you very much ill give it a try right now. The check() was given to me by another admin of the server and its suppose to check than if there is no file to write one.