table.delitem

Share your scripts and packages with other Mudlet users.
Post Reply
Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

table.delitem

Post by Phoenix »

What this does: Searches ipairs for a string, then deletes all the keys that == string. I need to do this in quite a number of scripts. It works just as well for one value that matches as for 20.
Code: [show] | [select all] lua
function table.delitem(tab, string) --(table), (string or number to remove) 
--I can't make it remove a nested table, it just returns '0 removed', and doesn't remove anything.
	if type(tab) == "table" then --makes sure we are working on a table
		if table.contains(tab, string) then --if the table contains it, let's run the script.
			local toremove = {} --initiate table to populate with keys to remove
			local rev = 0 --initiate our reverse number
			for key,val in ipairs(tab) do
				if val == string then table.insert(toremove, key) end --populate the table with keys that need to be removed
			end
			for _=1,#toremove  do --how many items are there in the table to remove? Loop this many times.
				table.remove(tab,toremove[#toremove-rev]) --start at the last key to remove, and work our way to the first. Avoids deleting things wrong.
				rev= rev+1 -- On the next loop, we'll be one farther back on the toremove table.
			end
			return #toremove --it will return the number removed
		else 
			return 0 --if the string was not in the table, it'll return 0
		end
	else error("Bad argment #1 to 'delitem'. (table expected, got "..type(tab)..")")
	end
end
Please tell me there is SOME easier way to do this??

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: table.delitem

Post by Vadi »

Code: [show] | [select all] lua
function table.delitem(tab, string)
  assert(type(tab) == "string", "Bad argment #1 to 'delitem'. (table expected, got "..type(tab)..")")

  for i,key in ipairs(tab do)
    if key == string then table.remove(i) return end
  end
end

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: table.delitem

Post by Rakon »

Might want not want to use the keyword 'string' as the argument/variable.
Code: [show] | [select all] lua
function table.delitem(tab, arg)
  assert(type(tab) == "table", "Bad argment #1 to 'delitem'. (table expected, got "..type(tab)..")")

  for i,key in ipairs(tab do)
    if key == arg then table.remove(i) return end
  end
end

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: table.delitem

Post by Phoenix »

Fixed that up so it -would- run... but still doesn't do what I wanted quite >.> I was getting the function to remove -all- of the the specified string. This will just do the first one, then end the function.

In order to remove them all, you gotta do it from the last key to remove to the first, otherwise you alter where the thing is in the table by removing the first, which makes you remove the wrong key when you try to remove the second, etcetera.
Code: [show] | [select all] lua
function table.delitem(tab, arg)
  assert(type(tab) == "table", "Bad argment #1 to 'delitem'. (table expected, got "..type(tab)..")")
  for i,key in ipairs(tab) do
	if key == arg then table.remove(tab, i) return end
  end
end
It did show me how to use assert proper though... wasn't able to get it to work before. With assert instead of if then else error end,
Code: [show] | [select all] lua
function table.delitem(tab, arg) --(table), (string or number to remove)
	assert(type(tab) == "table", "Bad argument #1 to 'delitem'. (table expected, got "..type(tab)..")")
	assert(type(arg) == "string" or type(arg) == "number", "Bad argument #2 to 'delitem'. (string or number expected, got "..type(arg)..")")
	if table.contains(tab, arg) then --if the table contains it, let's run the script.
		local toremove = {} --initiate table to populate with keys to remove
		local rev = 0 --initiate our reverse number
		for key,val in ipairs(tab) do
			if val == arg then table.insert(toremove, key) end --populate the table with keys that need to be removed
		end
		for _=1,#toremove  do --how many items are there in the table to remove? Loop this many times.
			table.remove(tab,toremove[#toremove-rev]) --start at the last key to remove, and work our way to the first. Avoids deleting things wrong.
			rev= rev+1 -- On the next loop, we'll be one farther back on the toremove table.
		end
		return #toremove --it will return the number removed
	else 
		return 0 --if the string was not in the table, it'll return 0
	end
end

Post Reply