Thread Tools Display Modes
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
05/30/14, 09:27 AM   #2
Stormknight
 
Stormknight's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 128
Tables are pretty awesome.

Something that often confuses new LUA coders - table entries start with an key/index of 1.

Lua Code:
  1. local newTable = {}
  2. table.insert(newTable,"First Line")
  3. d(newTable)

The contents of the table is a single entry, with a key of 1 (not zero, as you would expect with something like C++).
  Reply With Quote
05/30/14, 09:48 AM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Stormknight View Post
...

Something that often confuses new LUA coders - table entries start with an key/index of 1.

...
If you are learning Lua with knowledge of any other programming language, you may want to read this: http://www.luafaq.org/gotchas.html.


Warning: Spoiler
  Reply With Quote
05/30/14, 09:57 AM   #4
lyravega
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 93
Originally Posted by Garkin View Post
I'd use this:
Yeah, I'm trying to grasp my head around those stuff I am used to ternary operators from C, and just recently discovered that I can do the same with LUA's and/or stuff too (and more), which is neat

Lua Code:
  1. local alpha; alpha = allow and 0.5 or 1
  2. --if allow == true then alpha = 0.5 else alpha = 1 end
  Reply With Quote
05/30/14, 10:01 AM   #5
ingeniousclown
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 122
Originally Posted by lyravega View Post
Yeah, I'm trying to grasp my head around those stuff I am used to ternary operators from C, and just recently discovered that I can do the same with LUA's and/or stuff too (and more), which is neat

Lua Code:
  1. local alpha; alpha = allow and 0.5 or 1
  2. --if allow == true then alpha = 0.5 else alpha = 1 end
a = b ? c : d
is equal to
a = b and c or d
IFF
c is not "false" or "nil"

It's a minor but very important distinction.

Last edited by ingeniousclown : 05/30/14 at 10:03 AM.
  Reply With Quote
05/30/14, 10:10 AM   #6
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
Originally Posted by Stormknight View Post
Tables are pretty awesome.

Something that often confuses new LUA coders - table entries start with an key/index of 1.

Lua Code:
  1. local newTable = {}
  2. table.insert(newTable,"First Line")
  3. d(newTable)

The contents of the table is a single entry, with a key of 1 (not zero, as you would expect with something like C++).
But if you like 0 as your first element, then you can do:
Lua Code:
  1. table = { [0] = "firstElement", "secondElement", "thirdElement"}

Last edited by Harven : 05/30/14 at 10:12 AM.
  Reply With Quote
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
05/30/14, 04:38 PM   #8
lyravega
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 93
Originally Posted by Aiiane View Post
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]
It was just an example for table.remove, rather than a proper usage for (...)
  Reply With Quote
05/30/14, 04:41 PM   #9
Aiiane
 
Aiiane's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 19
Originally Posted by lyravega View Post
It was just an example for table.remove, rather than a proper usage for (...)
Right, but it's a bad example of table.remove, because that's not a situation you should be using table.remove in.
  Reply With Quote

ESOUI » Developer Discussions » Tutorials & Other Helpful Info » Some Table Tricks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off