like you would have list = {"item1", "item2", item3"}
how would I go about deleting just item2 and not 1 or 3
table.remove( t, n ) -- this will make all the entries after this shift down a number
-- or
t[n] = nil -- just blanks out that entry, and is slightly faster than ^
However, if you want to remove an entry by it's value, for example, removing "item2" from the table you have shown above, it's a little more complicated.
function remove( t, entry )
for i = #t, 1, -1 do -- a backwards loop, so if you remove an entry and the table length gets smaller it won't go past the end of the table
if t[i] == entry then -- if this index in the table is what you want to remove
table.remove( t, i )
-- use "break" here if you only want it to remove 1 occurence, and don't if you want to remove every occurence.
end
end
end
term.clear()
term.setCursorPos(1,1)
function load( path )
local file = fs.open(path, "r") --#don't use table as a variable, and variables cant be enclosed in quotes.
local todo = file.readAll()
file.close()
return textutils.unserialize(todo)
end
function remove(todo, entry)
for i = #todo, 1, -1 do
if todo[i] == entry then
table.remove(todo, i)
return
end
end
end
function save(todo, file)
local file = fs.open(file,"w") --#don't use table as a variable, and variables can't be enclosed in quotes.
file.write(textutils.serialize(todo))
file.close()
end
local todo = load(table) --#you didn't define todo
for k, v in pairs(todo) do
print(k..": "..v) --#fixed indentation
end
remove( todo, **any entry** ) --#You have to give remove arguments, otherwise it will error
save(todo,"table") --#table was a variable, now it's a string.
Also worth reading this scope tutorial. Localising todo to load() won't work out so well.
local file = fs.open(path, "r")There are a few errors in your script. Here they are:term.clear() term.setCursorPos(1,1) function load( path ) local file = fs.open(path, "r") --#don't use table as a variable, and variables cant be enclosed in quotes. local todo = file.readAll() file.close() return textutils.unserialize(todo) end function remove(todo, entry) for i = #todo, 1, -1 do if todo[i] == entry then table.remove(todo, i) return end end end function save(todo, file) local file = fs.open(file,"w") --#don't use table as a variable, and variables can't be enclosed in quotes. file.write(textutils.serialize(todo)) file.close() end local todo = load(table) --#you didn't define todo for k, v in pairs(todo) do print(k..": "..v) --#fixed indentation end remove( todo, **any entry** ) --#You have to give remove arguments, otherwise it will error save(todo,"table") --#table was a variable, now it's a string.
ah ok so what would be a valid argument or entry to remove? like would I put 2 as the argument for the second one and 3 as the 3rd one and so on? or would I actually type the entire string for that part of the table that I want to remove?