View Single Post
05/30/14, 08:45 AM   #1
lyravega
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 93
Question Some Table Tricks

I like tables. A lot. Though they confuse me sometimes, and a LOT if there is a table-ception to speak of :P Anyway, here are some table tricks that might come handy. These already exist in some other form, by the way, I am kinda used to use these ways.

-"table.insert" is not always necessary. Nor "table.remove". Setting a key to nil will "delete" it from the table. Setting a table to nil will "delete" the table. Setting a key to something will create that key in the table. HOWEVER, these two functions will also increment/decrement the ordered numeric array keys (or fill in the blanks, if there are any), and properly adjust the "#tableElements"; meaning that if you want a proper array, and use "#tableElements", you shouldn't do it this way.

-"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, or transfer elements between tables with ease.

-If you are working with table, where you need to check if something exists in a table, you can avoid using loops. Instead of looking for the value, look for the key. You will need to use the value name like a key though; if it is a simple array for example, this thing is not helpful in any way.

Lua Code:
  1. local newTable = {["key"] = 5}; if newTable.key then d(true) else d(false) end;

This will return true if key is in the table (in other words, it exists), else it will return false. Value doesn't have to be boolean, it can be any type of variable, even another table as well. Note that, if you are not using an array, "#tableSize" will not work if you are using it.

The table can be as complex as it can be, but you can still use this. You may need to use [] to work with key-values effectively. "table.element" is same for "table["element"]" but "table[element]" is different; the element in the last one is a variable, not a key. You can basically create a local string and take advantage of it.

Lua Code:
  1. local test = "herpderp";
  2. d(newTable.test) --prints the test key's value in the table (assuming all are present)
  3. d(newTable["test"]) --does the same as above
  4. d(newTable[test]) --prints the herpderp's value in the table; [u]NOT[/u] the test's

-To combine some tips here, you can check if a key-value exists in a table with a simple if, and if it doesn't, you can just create it by assigning something to that key-value real fast. For example:

Lua Code:
  1. local derp = "testValue3";
  2. local newTable = {["testValue"] = "HI THERE"};
  3. if not newTable.testValue then newTable.testValue = "NOPE" --will not do it, as testValue is present
  4. if not newTable.testValue2 then newTable.testValue2 = true end --now we have another key-value in this table
  5. if not newTable[derp] then newTable[derp] = 5 end -- and now we have another
  6.  
  7. --final table:
  8. --newTable = {
  9. --["testValue"] = "HI THERE",
  10. --["testValue2"] = true,
  11. --["testValue3"] = 5,
  12. --}

-As these key-values are not ordered numbers, these tables are... well they are tables and not arrays, and "#table" will return 0. Quick counter for this:

Lua Code:
  1. local newTableSize = 0
  2. for key in next, newTable do
  3. newTableSize = newTableSize + 1
  4. end

"next" is basically included in "pairs"; there is no difference if you use the for loop with pairs. I don't know if ipairs will work, or if it'd be beneficial, as ipairs are more suited to real, ordered arrays.

Last edited by lyravega : 05/30/14 at 04:40 PM.
  Reply With Quote