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.