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

Table not working properly, possibly bad format or use of tables?

Started by JinglebellsMan9, 18 July 2012 - 05:31 PM
JinglebellsMan9 #1
Posted 18 July 2012 - 07:31 PM
Hello computercraft forums, can someone please explain to me why isn't this working correctly.

Problem: When borrowing book, the re-assignment of the bookID's value does not actually update to "borrowed". It just simply stays as "here" (unborrowed)

My guess of the error: Possible incorrect use of reassigning the value to key?

If anyone can enlighten me with the actual error/solution to this then I would be absolutely delighted. (Aswell as any other tips, that would be nice as well!) :P/>/>


function borrowbook()
print("Please enter the book id of the book you want to borrow.")
bookid = read()
if books.bookid==here then
books[bookid] = "borrowed"-- <<<=== for some odd reason, this guy doesn't actually make key "1" 's value into "borrowed"
print("Book: ",bookid," is now borrowed...") -- let user know the book he choice is now borrowed by him/her.

-- Quick debug info
for k, v in ipairs(books) do
print("Book: ", k, " State: ", v)
end
print("---debug seperator line--")
junkvar = tostring(io.read())

-- Quick debug info
else
print("That book is already borrowed by someone.") -- if book is "borrowed" then tell user he can't borrow it.
print(books.bookid) -- Tell user what book he/she tried borrowing
end
sleep(1)
main()
end

function returnbook() -- This function will be fixed/updated once I get "book" function to work.
print("Please enter the book id of the book you want to return.")
unbookid = read()
if books[unbookid]=="borrowed" then
books[unbookid]="here"
else
print("That book is already borrowed by someone.")
end
main()
end

function status()
for k, v in ipairs(books) do
print("Book: ", k, " State: ", v)
end
junkvar = tostring(io.read()) --This is so user can read the list of books' statuses until he/she press a key.
main()
end

function re() -- This actually didn't work... Will remove later.. (Was only used to quick reloading of the program)
shell.run(lib.txt)
end

function main()
term.clear()
term.setCursorPos(1,1)
print("Library Computer (IBOP)")
print("Do you want to borrow a book, return a book or see status of books?")
print("")
term.setCursorPos(1,4)
userchoice = tostring(io.read())
if userchoice == "borrow" then
borrowbook()
elseif userchoice== "return" then
returnbook()
elseif userchoice == "status" then
status()
elseif userchoice == "re" then
re()
end
end

--=============== Function stuff above, program startup code below===========

term.clear()
term.setCursorPos(1,1)
books = {} -- Creates table called "books"

for z=1, 12 do --This guy assigns table "books" some keys and values.
books[z] = "here"
end

main()

-- To do: 1. Add code to borrowing and returning functions to activate dispenser. 2. Add code to open secrect librarian door. 3. Add exit program option for userchoice. 4. change program name to startup incase server restarts. 5.Refine script?
MysticT #2
Posted 18 July 2012 - 07:53 PM
Errors commented on the code:

function borrowbook()
  print("Please enter the book id of the book you want to borrow.")
  bookid = read() -- you may want to use tonumber(read()) to use numerical ids
  if books.bookid == here then -- should be: if books[bookid] == "here" then
    books[bookid] = "borrowed"
    print("Book: ",bookid," is now borrowed...")

-- Quick debug info
for k, v in ipairs(books) do
print("Book: ", k, " State: ", v)
end
print("---debug seperator line--")
junkvar = tostring(io.read())

-- Quick debug info
  else
    print("That book is already borrowed by someone.")
    print(books.bookid) -- should be: print(books[bookid])
  end
  sleep(1)
  main()
end

function returnbook()
  print("Please enter the book id of the book you want to return.")
  unbookid = read() -- same as above, use tonumber(read()) if you want numerical indices
  if books[unbookid] == "borrowed" then
    books[unbookid]="here"
  else
    print("That book is already borrowed by someone.")
  end
  main()
end

function status()
  for k, v in ipairs(books) do
    print("Book: ", k, " State: ", v)
  end
  junkvar = tostring(io.read()) -- you can use os.pullEvent("key") to wait for a key press
  main()
end

function re()
  -- really bad idea to re-run the program, use a loop instead
  shell.run(lib.txt) -- you forgot the quotes, it should be: shell.run("lib.txt")
end

function main()
  term.clear()
  term.setCursorPos(1,1)
  print("Library Computer (IBOP)")
  print("Do you want to borrow a book, return a book or see status of books?")
  print("")
  term.setCursorPos(1,4)
  userchoice = tostring(io.read()) -- read returns a string, no need to use tostring here
  if userchoice == "borrow" then
    borrowbook()
  elseif userchoice== "return" then
    returnbook()
  elseif userchoice == "status" then
    status()
  elseif userchoice == "re" then
    re()
  end
end

--=============== Function stuff above, program startup code below===========

term.clear()
term.setCursorPos(1,1)
books = {} -- you should define this as local at the top of the program

for z = 1, 12 do
  books[z] = "here"
end

main()
The problems are:
1) You assign numerical indices to the books, but then try to use string keys (read() returns a string). So the book can't be found.
2) Using books.bookid is like doing books["bookid"] (note the quotes, it's the string "bookid"), but you need to use the contents of the bookid variable, so you have to use books[bookid]
3) The most important thing, don't re-run your program, and don't call the main function again. That will crash the computer after some time, use loops instead.
JinglebellsMan9 #3
Posted 18 July 2012 - 08:15 PM
Ah, now I see my errors. :)/>/>
Thank you for such a speedy response!

I put the program startup code at the end because I thought computercraft needs to read from top to bottom. So if I put it at the top then the computer wouldn't know functions existed, seems like what I thought is incorrect. Hehe. :P/>/>


How would I use "loops" to return to the main_screen to avoid calling the main function/re-running the program?

Edit: Oh, and the script works perfectly now with the errors corrected.
MysticT #4
Posted 18 July 2012 - 08:45 PM
Ah, now I see my errors. :)/>/>
Thank you for such a speedy response!

I put the program startup code at the end because I thought computercraft needs to read from top to bottom. So if I put it at the top then the computer wouldn't know functions existed, seems like what I thought is incorrect. Hehe. :P/>/>


How would I use "loops" to return to the main_screen to avoid calling the main function/re-running the program?

Edit: Oh, and the script works perfectly now with the errors corrected.
You'r right, it reads from top to bottom. What I meant is that you should define any function/variable at the top of the program and make them local.
For the loop, it would be better to use a while loop.
It should be something like:

local someVar = 10
local aTable = {}

local function myFunction()
  -- do something
end

-- Start Program

while true do -- infinite loop
  local input = getInput() -- get input somehow
  if input == "do something" then
    -- do something here
  elseif input == "exit" then
    break -- stop the loop to exit the program
  end
end
JinglebellsMan9 #5
Posted 18 July 2012 - 08:48 PM
Ahh, I see… That clears everything up now. :)/>/>
I'll make the additional changes to my program and report back. :P/>/>

Edit: I added other features that I was going to implement in (such as my secret door to the librarian room), I made the variables local, and I made the main screen a "while true loop" (Infinity!!! Yay. lol)
I also see why variables and functions should be local now, I forgot to remove one of the "main()" calls and it managed to call the old function from another .txt file on the same computer. I guess it could be really missleading in debuging and testing programs it one of the old backup versions of the same program is still there with the old function.

Learned a lot today and acomplished alot today. Awesome day I would think, yes?