Posted 22 May 2013 - 01:58 PM
Title: [LUA] adding to a table within a table
I'm trying to write a program that can display various information on a monitor. It's going to display a list of projects that we are currently working on on the server.
click for picture
If you click new project it adds a new square and write whatever input you make and saves that to the table "project" which it then saves to the file "projects" on the computer.
If you click on one of the projects it opens that project.
click for picture
Allmost the same deal here. It displays (or at least is supposed to) the names of those who are working on that project and gives the opportunity to add a name as above.
But here comes my problem;
first of all, right now it's not displaying this last screen correct. If no names are present it's supposed to write "No one is working on this project", otherwise it should display the buttons with names of those who are working on it.
second, if i try and add a new name i get "board:63: bad argument: table expected, got nil".
If have been looking at this for hours now, trying different solutions but nothing is really working.
I'm pretty sure it's the way i handle tables within tables as the "project" screen works just fine.
What i'm trying to do is add the entered name to a numbered table that is inside the table "names". The numbered table corresponds to the same number the index from the "project" table has.
If someone comes up and add themself to that project their names should be added to name[–project name] [–next unused index]
so name[2][3] should display church as the project and the third person to write their name to the project.
also the way i'm adding a new name to the 'names' table seems wrong. I need to delete the nameList afterwards so it won't copy old names to a new project.
as it is now:
would this empty the nameList table?
I'm not sure this is the best way to go about it, but this was the best way i could think of.
i hope it's clear what i'm trying to accomplish and what my problem is.
code - errors are commented with red and begins with "#########"
this is what the file called names looks like
and this is the projects file
a simplified version of the code, except now i try to insert input directly into names[–project], but getting the same error:
I'm trying to write a program that can display various information on a monitor. It's going to display a list of projects that we are currently working on on the server.
click for picture
Spoiler
If you click new project it adds a new square and write whatever input you make and saves that to the table "project" which it then saves to the file "projects" on the computer.
If you click on one of the projects it opens that project.
click for picture
Spoiler
Allmost the same deal here. It displays (or at least is supposed to) the names of those who are working on that project and gives the opportunity to add a name as above.
But here comes my problem;
first of all, right now it's not displaying this last screen correct. If no names are present it's supposed to write "No one is working on this project", otherwise it should display the buttons with names of those who are working on it.
second, if i try and add a new name i get "board:63: bad argument: table expected, got nil".
If have been looking at this for hours now, trying different solutions but nothing is really working.
I'm pretty sure it's the way i handle tables within tables as the "project" screen works just fine.
What i'm trying to do is add the entered name to a numbered table that is inside the table "names". The numbered table corresponds to the same number the index from the "project" table has.
If someone comes up and add themself to that project their names should be added to name[–project name] [–next unused index]
so name[2][3] should display church as the project and the third person to write their name to the project.
also the way i'm adding a new name to the 'names' table seems wrong. I need to delete the nameList afterwards so it won't copy old names to a new project.
as it is now:
function getInputName(project) -- read user input and inserts it to table in names table.
input = read()
table.insert(nameList, input)
table.insert(names[project], nameList)
end
would this empty the nameList table?
function getInputName(project) -- read user input and inserts it to table in names table.
input = read()
table.insert(nameList, input)
table.insert(names[project], nameList)
for k,v in pairs(nameList) do
nameList[k]=nil
end
end
I'm not sure this is the best way to go about it, but this was the best way i could think of.
i hope it's clear what i'm trying to accomplish and what my problem is.
code - errors are commented with red and begins with "#########"
Spoiler
side = "right"
pSide = "top"
mon = peripheral.wrap(side)
w, h = mon.getSize()
project = {}
mainMenu = {"Rules", "View projects"}
yourProjects = {}
names = {}
nameList = {}
screen = 0
player = peripheral.wrap(pSide)
function checkFile(path) -- check if file exists
if not fs.exists(path) then
file = fs.open(path, "w")
file.close()
end
end
function writeFile(fileName, tableName) -- overwrites an old file
file = fs.open(fileName, "w")
file.write(textutils.serialize(tableName))
file.close()
end
function loadProjects(load) -- Load filed called projects
checkFile(load)
file = fs.open(load, "r")
readFile = file.readLine()
if readFile ~= nil then
project = textutils.unserialize(readFile)
end
file.close()
end
function loadNames(load) -- load file called names
checkFile(load)
file = fs.open(load, "r")
readFile = file.readLine()
if readFile ~= nil then
name = textutils.unserialize(readFile)
end
file.close()
end
function getInputProject(projectName) -- read user input and inserts it to the project table
input = read()
table.insert(project, input)
end
function getInputName(project) -- read user input and inserts it to table in names table.
input = read()
table.insert(nameList, input)
table.insert(names[project], nameList) --[color=#ff0000] ######### This is the error line[/color]
end
function drawImage(image, x, y)
myImage = paintutils.loadImage(image)
paintutils.drawImage(myImage, x, y)
end
function viewProjects() -- displays stored projects
x = 1
y = 1
mWrite("New project", (w-11)/2, h, colors.lightGray)
if table.getn(project) > 0 then
for i = 1, #project do
drawImage("button", x, y)
cY = y +2
cX = x+1
y = y + 4
mWrite(project[i], cX, cY, colors.lime)
if y >= h - 4 then y = 1 x = x+12 end
end
else
mWrite("There are currently no projects", (w-31)/2, h/2, colors.white)
end
end
function startMenu() -- displays start menu
drawImage("header", 1, 1)
mWrite("Build Board", (w-11)/2, 2, colors.lightGray)
x = (w/8) -1
y = h/2
cY = y+1
cX = x +5
for i = 1, #mainMenu do
drawImage("main", x, y)
mWrite(mainMenu[i], cX, cY, colors.white)
cX = cX + 16
x = x+20
end
end
function drawBoard() -- decides which menu to draw
term.clear()
if screen == 0 then
startMenu()
touch()
elseif screen == 1 then
viewProjects()
inputProject()
end
end
function mWrite(message, cX, cY, background) -- writes message on cX and cY with background as background color
mon.setBackgroundColor(background)
mon.setTextColor(colors.black)
mon.setCursorPos(cX, cY)
mon.write(message)
mon.setBackgroundColor(colors.black)
end
function touch() -- Toggle between menues
event, param1, x, y = os.pullEvent()
if event == "monitor_touch" then screen = screen +1
if screen == 2 then screen = 0 end
end
end
function inputProject() -- add a project to the list
event, param1, x, y = os.pullEvent()
if event == "monitor_touch" and x > (w-11)/2 and x < (w+11)/2 and y == h then
getInputProject(project)
writeFile("projects", project)
elseif event == "monitor_touch" and x == w and y == h then screen = screen +1
if screen == 2 then screen = 0 end
else clickProject()
end
end
function clickProject() -- determines which project is clicked
if event == "monitor_touch" and x > 1 and x < 12 and y > 1 and y < 5 and table.getn(project) > 0 then enterProject(1)
elseif event == "monitor_touch" and x > 1 and x < 12 and y > 5 and y <9 and table.getn(project) > 1 then enterProject(2)
elseif event == "monitor_touch" and x > 1 and x < 12 and y > 9 and y < 13 and table.getn(project) > 2 then enterProject(3)
elseif event == "monitor_touch" and x > 1 and x < 12 and y > 13 and y < 17 and table.getn(project) > 3 then enterProject(4)
elseif event == "monitor_touch" and x > 1 and x < 12 and y > 17 and y < 21 and table.getn(project) > 4 then enterProject(5)
elseif event == "monitor_touch" and x > 1 and x < 12 and y > 21 and y < 25 and table.getn(project) > 5 then enterProject(6)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 1 and y < 5 and table.getn(project) > 6 then enterProject(7)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 5 and y <9 and table.getn(project) > 7 then enterProject(8)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 9 and y < 13 and table.getn(project) > 8 then enterProject(9)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 13 and y < 17 and table.getn(project) > 9 then enterProject(10)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 17 and y < 21 and table.getn(project) > 10 then enterProject(11)
elseif event == "monitor_touch" and x > 13 and x < 24 and y > 21 and y < 25 and table.getn(project) > 11 then enterProject(12)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 1 and y < 5 and table.getn(project) > 12 then enterProject(13)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 5 and y < 9 and table.getn(project) > 13 then enterProject(14)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 9 and y < 13 and table.getn(project) > 14 then enterProject(15)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 13 and y < 17 and table.getn(project) > 15 then enterProject(16)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 17 and y < 21 and table.getn(project) > 16 then enterProject(17)
elseif event == "monitor_touch" and x > 36 and x < 12 and y > 21 and y < 25 and table.getn(project) > 17 then enterProject(18)
elseif event == "monitor_touch" and y == h and x == w then screen = screen +1
if screen == 2 then screen = 0 end
end
end
function enterProject(projectName) -- see who is working on clicked project
while true do
term.clear()
drawImage("header", 1,1)
mWrite(project[projectName], (w-#project[projectName])/2, 2, colors.lightGray)
mWrite("Add name", (w-8)/2, h, colors.lightGray)
x = 1
y = 5
if names[projectName] ~= nil then
if table.getn(names[projectName]) > 0 then
for i = 1, #names[projectName] do
drawImage("button", x, y)
cY = y +2
cX = x+1
y = y + 4
mWrite(names[projectName][i], cX, cY, colors.lime) --[######### this is where it writes the names.]
if y >= h - 4 then y = 1 x = x+12 end
end
else
mWrite("No one is working on this project", (w-33)/2, h/2, colors.white)
end
end
inputName()
if backToMain == true then
backToMain = false
break
end
end
end
function inputName() -- add a name to a project and save it to file "names"
event, param1, x, y = os.pullEvent("monitor_touch")
if event == "monitor_touch" and x > (w-11)/2 and x < (w+11)/2 and y == h then
getInputName(projectName)
writeFile("names", names)
elseif event == "monitor_touch" and x > w-2 and y > h-2 then
screen = screen +1
backToMain = true
if screen == 2 then screen = 0 end
end
end
---------- LOOP -------------
while true do
loadProjects("projects")
loadNames("names")
drawBoard()
end
this is what the file called names looks like
Spoiler
{[1] = {[1] = "ravn",},}
and this is the projects file
Spoiler
{[1] = "casino", [2] = "church", [3] = "stores", }
a simplified version of the code, except now i try to insert input directly into names[–project], but getting the same error:
Spoiler
--test
project = {}
names = {}
function getInputProject(projectName)
input = read()
table.insert(project, input)
end
function getInputName(project)
input = read()
table.insert(nameList, input)
table.insert(names[project], input)
end
print("enter project")
getInputProject()
print("a new project")
getInputProject()
print("add a name to project2")
getInputName(2)
print("and one to project1")
getInputName(1)
term.clear()
print(project)
print(names)
print(names[1][1])
print(names[2][1])
print(names[1])
print(names[2])
Edited on 22 May 2013 - 01:26 PM