Jump to content




deleting entries in tables?


  • You cannot reply to this topic
10 replies to this topic

#1 subzero22

  • Members
  • 97 posts

Posted 03 November 2014 - 10:03 AM

I know how to add to and save tables but I can't for the life of me figure out how to delete something from a table.

like you would have list = {"item1", "item2", item3"}

how would I go about deleting just item2 and not 1 or 3

#2 Exerro

  • Members
  • 801 posts

Posted 03 November 2014 - 10:57 AM

To remove a number index, it's very simple, just do
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.
You must loop through the table and check each value.
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


#3 subzero22

  • Members
  • 97 posts

Posted 03 November 2014 - 11:05 AM

ok I think I understand I'll do some fiddling and see if I can figure it out. If not i'll post back here with a code of what I tried.

#4 subzero22

  • Members
  • 97 posts

Posted 03 November 2014 - 12:28 PM

ok I keep getting todo[12] attempt to get length of nil

Here's the code I'm using to test what you gave me yKLxCxYq

in file table I have {"item 1", "item 2", "item 3"}

I did have todo = in the file but when I ran a test for just read and then save it took the todo = out.

Edit: Oh ya also I know that the 1 and -1 selects the second item but what if there's 4 or 5 entries?

trying to make the code so someone can select an entry from a menu to delete. Sorry I forgot to say that before.

Edited by subzero22, 03 November 2014 - 12:48 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 03 November 2014 - 01:09 PM

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.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 03 November 2014 - 01:27 PM

Also worth reading this scope tutorial. Localising todo to load() won't work out so well.

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 03 November 2014 - 01:35 PM

View PostBomb Bloke, on 03 November 2014 - 01:27 PM, said:

Also worth reading this scope tutorial. Localising todo to load() won't work out so well.

Actually, he returns the unserialized version. The raw file contents aren't going to matter if we can see them or not.

#8 subzero22

  • Members
  • 97 posts

Posted 03 November 2014 - 01:49 PM

View PostKingofGamesYami, on 03 November 2014 - 01:09 PM, said:

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.
local file = fs.open(path, "r")
If I try it without quates then it doesn't read the file for some reason and gives me an error also table is the file name but if that messes with the code then I'll rename the file to something else.


local todo = load(table) --#you didn't define todo
for k, v in pairs(todo) do
print(k..": "..v) --#fixed indentation
end


not sure what you mean by I didn't define todo as it still lists the stuff inside the table. I'm adding it after calling the load function.

I know your helping and I'd just like more info on why it works but still an error? right now the problem I'm having is with the remove function.

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 03 November 2014 - 02:11 PM

Uhm, right now you aren't giving the remove function valid arguments. I didn't give it an entry, because I don't know which entry you want to remove!

#10 subzero22

  • Members
  • 97 posts

Posted 03 November 2014 - 03:28 PM

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?

#11 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 03 November 2014 - 03:35 PM

View Postsubzero22, on 03 November 2014 - 03:28 PM, said:

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?

The way your remove function is set up, you need to provide two arguments
1 - the table to remove the value from
2 - the value you wish to remove

The value you wish to remove isn't the key, but you can set it up that way. In fact, you wouldn't need your own function if you did it that way...

Also, I noticed you wanted the user to select something. You might want to look at my simple menu program.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users