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

Code Not Runnig Correctly

Started by Termanater13, 25 September 2013 - 12:03 AM
Termanater13 #1
Posted 25 September 2013 - 02:03 AM
I'm trying to test some code and all I keep getting is it printing the number 23 rather tan running.
The code itself is not complete, but is in a state I can begin testing it.

-- Setup
mon = peripheral.wrap("right")
mon.clear()
mon.setTextScale(.5)
mon.setCursorPos(1,1)
current = 0
numtest = 1
-- does a list already exist if so use it, otherwise start a new
if fs.exists("ToDo") then
  list = termAPI.load("ToDo")
else
  list = {}
end
-- sone test list variabels
list[1] = "test"
list[3] = "test"
-- checks the list for the number of them and that the numbers for them are correct
for k,v in ipairs(list)
  if type(k) == "number" then
	numtest = numtest + 1
  end
  if k == numtest then
  else
	list[numtest] = v
  end
end
list["total"] = numtest
-- used to display to the monitor
function display()
  mon.setCursorPos (7,1)
  mon.write("--== Things To Do ==--")
  current = 1
  for k,v in ipairs(list) do
	current = current + 1
	mon.setCursorPos(1,current)
	mon.write(k ..": ".. v)
  end
end
-- main part of the code
while true do
  display()
  print("type \"new\" for new To Do item")
  print("type \"done\" when an Item is done")
  print("type \"exit\" to exit APP")
  test = read()
  if test == "new" then

  elseif test == "done" then

  elseif test == "exit" then
	break
  else
	print("command not reconized")
	print("press any key to continue")
  end
end
MKlegoman357 #2
Posted 25 September 2013 - 09:17 AM
Line 18, You missed "do".
Line 22, use "~=" , because 'else' breaks the computer.
Localize variables and functions:

local a = "Apple"

local function me ()
  print(a)
end
LBPHacker #3
Posted 25 September 2013 - 09:41 AM
'else' breaks the computer
Nope.

But yeah, use ~=. Using else that way is pointless.
AgentE382 #4
Posted 25 September 2013 - 09:51 AM
….
Line 22, use "~=" , because 'else' breaks the computer.
`else` does nothing of the sort. Certainly, the code will be more readable when written with `~=`, but the way that statement is now should run fine.

elseif test == "exit" then
break
else
print("command not reconized")
print("press any key to continue")
end
end
If you want your "press any key to continue" to work, put an `os.pullEvent("key")` after your `print` statement.

Oh, and MKlegoman357 is right. That's probably your issue.

In the future, please post the full error message. Otherwise, people won't want to wade through your code to find a simple bug that could have been fixed by looking up the error message in this thread, or this thread.
MKlegoman357 #5
Posted 25 September 2013 - 11:09 AM

….
Line 22, use "~=" , because 'else' breaks the computer.
`else` does nothing of the sort.

You both misunderstood me. I was testing this code in Minecraft to find out what was happening. I found that 'do' was missing. When I tried running the code, it froze the computer and then computer shutdown automatically, without any error or anything. When I deleted 'else' the program started to work. I think something is wrong when 'if then else' is empty or something.
Termanater13 #6
Posted 25 September 2013 - 12:40 PM
In the future, please post the full error message. Otherwise, people won't want to wade through your code to find a simple bug that could have been fixed by looking up the error message in this thread, or this thread.
Im not getting an error message, all I get is "23" printed to the screen. It is not running as intended

As for everyone's suggestions for improvement, I'll implement them. Just keep in mind that it is still being worked on, and even in its current state it is nor running as intended.
MKlegoman357 #7
Posted 25 September 2013 - 02:35 PM
How are you getting 23? My computer goes to "To long without yielding…" (the message doesn't appear, computer just freezes and shutdowns).

I found a problem:
You add something to a table that is used by ipairs in a for loop. When you add a key and a value to a table that is used by ipairs or pairs in a for loop, it "confuses" sometimes and starts giving you unexpected results. When I was testing it I found that in your first for loop you add strings into a table called list. When for loop returns next key and value it returns that recently created value, copies it to next key –> for loop returns next key and value it returns that recently created value, copies it to next key…

I don't know how to explain that to you, if somebody understands what is happening, please explain.
Termanater13 #8
Posted 25 September 2013 - 03:00 PM
How are you getting 23? My computer goes to "To long without yielding…" (the message doesn't appear, computer just freezes and shutdowns).

I found a problem:
You add something to a table that is used by ipairs in a for loop. When you add a key and a value to a table that is used by ipairs or pairs in a for loop, it "confuses" sometimes and starts giving you unexpected results. When I was testing it I found that in your first for loop you add strings into a table called list. When for loop returns next key and value it returns that recently created value, copies it to next key –> for loop returns next key and value it returns that recently created value, copies it to next key…

I don't know how to explain that to you, if somebody understands what is happening, please explain.
I understand, I can fix this by using another table to edit what the list is using then when outside the loop over right the first.

-=EDIT=-
Did a bit of a rewrite to it, to simplify a few things, and added what people said. I alost made the same mistakes that broght me here in the first place, still some testing needs to be done befr I call it done
-- start up script / App Global Vars
mon = peripheral.wrap("right")
mon.setTextScale(0.5)

-- check for existing To Do List
if fs.exists("ToDo") then
    list = termAPI.load("ToDo")
else
    list = {}
    list["total"] = 0
end
-- check list function
check = function (copy)
    copy["total"] = 0
    for k,v in ipairs(list) do
        if type(k) == "number" then
            copy["total"] = copy["total"] + 1
            copy[copy["total"]] = v
        end
    end
    return copy
end
-- display code to print to do to World Monitor, and only the world Monitor
display = function ()
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("--== To DoList ==--")
    mon.setCursorPos(1,2)
    mon.write("Use Terminal to edit list")
    for k,v in ipairs(list) do
        if type(k) == "number" then
            mon.setCursorPos(1,(k+3))
            mon.write(k .. ": " .. v)
        end
    end
end
-- for user input to controle the list
user = function ()
    term.clear()
    term.setCursorPos(1,1)
    print("--== To Do List Editor ==--")
    print("type \"new\" to create new Item")
    print("type \"done\" when a Item is complete")
    print("type \"exit\" to exit App")
    print("press enter to refresh monitor")
    input = read()
    if input == "new" then
        term.clear()
        term.setCursorPos(1,1)
        print("What do you want to add to the list?")
        add = read()
        list[list["total"]+1] = add
    elseif input == "done" then
        term.clear()
        term.setCursorPos(1,1)
        print("what is the number of the Item that is completed? Tip: If more then one is complete use largest number first")
        done = read()
        done = tonumber(done)
        list[done] = nil
    elseif input == "exit" then
        return "break"
    elseif input == "" or input == nil then
        return nil
    else
        term.clear()
        term.setCursorPos(1,1)
        print("input not reconized")
        print("press any key to continue")
        os.pullEvent("key")
    end
end
-- Main Loop
while true do
    list = check(list)
    display()
    test = user()
    if test == "break" then
        break
    end
    termAPI.save("ToDo",list)
end
end
–== EDIT(2) ==–
The rewrite and the info you guys gave fixed the issue(I almost made the mistake that caused the "To long without yielding…" error again). I made sure to follow what you said and its now fixed, but I'm still having issues not related to the Original post.
now when I tell the system that an item is done it deletes all Items after it as well. lates say I have 10 items in the list and I tell it item 6 is done it deletes Items 6-10, and not just Item 6. I think it may have something to do with the check function.
MKlegoman357 #9
Posted 26 September 2013 - 08:39 AM
The rewrite and the info you guys gave fixed the issue(I almost made the mistake that caused the "To long without yielding…" error again). I made sure to follow what you said and its now fixed, but I'm still having issues not related to the Original post.
now when I tell the system that an item is done it deletes all Items after it as well. lates say I have 10 items in the list and I tell it item 6 is done it deletes Items 6-10, and not just Item 6. I think it may have something to do with the check function.

Use table.remove(), it will remove an item and sort them if needed.