Table Remove

Post Reply
icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Table Remove

Post by icesteruk »

I seem to be having an issue, I get how table.insert works, but somehow i cant do


if table.isMember(fociinarea, matches[2]) then
table.remove(fociinarea, matches[2])
end

the matches[2] is one of the different names in the fociinarea table...

I gathered you cant remove a word from the table and just the 'number' of the position it is in, so my question is.. how do you remove the number or find which number it is? im well confused..

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Table Remove

Post by Delrayne »

You have to loop through the table.
Code: [show] | [select all] lua
for k, v in ipairs (fociinarea) do
   if (v == matches[2]) then
      table.remove(fociinarea, k)
   end
end

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: Table Remove

Post by icesteruk »

Thank you!!

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Table Remove

Post by Golem »

Interestingly enough there is a TableUtils.lua included in mudlet-lua, and therein you can find
such functions (not yet documented it seems) like listRemove. This would simplify your code to this:
Code: [show] | [select all] lua
listRemove(fociinarea, matches[2])
Good luck, G.

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

Re: Table Remove

Post by Vadi »

There's no need to use them, they're left there for backwards compatibility - table.remove() is better to use.

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Table Remove

Post by Golem »

Not to be nitpicky, since you said that those functions are left there for backwards compatibility, but usually it is better to use already included code, especially when it does exactly the same thing as the aforementioned solution using table.remove.

On the other hand i do think that including some more usefull functions in TableUtils might be not a bad idea for the future, since it will simplify things for "lua newbies", but that is up to you guys.

Regards, G.

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

Re: Table Remove

Post by Vadi »

Yeah, the included code, that's documented everywhere and way more, is table.remove(). The list functions just duplicated table functions while offering no extra help and just giving the confusion of choice between the same equivalents.

They're left in there because we don't break peoples scripts, so whomever used them before still can.

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Table Remove

Post by Golem »

Table.remove does not accept the element, it accepts the key, which I'm sure you are aware of. So no, listRemove even though named poorly ;) does not duplicate table.remove functionality.

Sure, probably it could have been named table.removeelem... :)

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

Re: Table Remove

Post by Vadi »

Ahh, my fault. There's the table.remove, table.index_of pattern instead... hm.

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Table Remove

Post by Golem »

I considered it, but it does remove only one occurence. Still, since mudlet already offers index_of it might offer table.removeelem while retaining backward compatibility by a simple assignment to listRemove :)

Post Reply