View Single Post
05/30/14, 04:19 PM   #7
Aiiane
 
Aiiane's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 19
Originally Posted by lyravega View Post
-"table.remove(tableName, elementPos)" also returns the removed element
By default, it returns the last element in the table, but you can specify which element by specifying the "elementPos". You can construct and use queues easily with this handy function.

Additionally, you can use this with "...". Similar to "select()", this usage is. Lets say there is an event that returns 20 things at once, and you need 4th and 17th elements among that many. Instead of using a lot of "_"'s you can just define 2 local variables, and use table.remove as like this:

Lua Code:
  1. local element4, element17 = table.remove({...},4), table.remove({...},17)

Note that this isn't the best use of this "...", as there are two table.remove functions both creating a new table from "..." to remove an element. Better use would be creating a new local table for "..." first, then removing the elements from it. If we had done so on the above example, the second remove function would need to remove 16th element rather than 17 this time.
This is rather inefficient. If you just want to get the values at certain indices, just index them! There's no reason to remove values from the table.

Lua Code:
  1. local dottable = {...}
  2. local element4, element17 = dottable[4], dottable[17]
  Reply With Quote