View Single Post
05/12/14, 05:36 PM   #5
stjobe
 
stjobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Post

I'm not sure what you're trying to achieve, as nilling the table effectively deletes it (it will be garbage collected eventually).

Lua Code:
  1. for k,_ in pairs(someTable) do
  2.     someTable[k] = nil
  3. end
should do the trick; someTable is now empty and will be disposed of in time.

As for printing out the values of your table containing tables and functions, there's a slight hitch; you can't get the source code for functions back after compilation to byte-code. Tables should be easy enough though:

Example:
Lua Code:
  1. t = {}
  2.  
  3. function t.someFunction()
  4.     local var = 13
  5.     print(var)
  6. end
  7.  
  8. t.someTable = {}
  9. t.someTable.alpha = "tableEntry 1"
  10. t.someTable.beta = "tableEntry 2"
  11.  
  12. for k,v in pairs(t) do
  13.     print(k) -- gives the key, e.g. "someTable" or "someFunction"
  14.     print(v) -- gives the value, e.g. "table: 0x04E50778" or "function: 0x04EF298"
  15.     print(t[k]) -- same as "print(v)"
  16.     if type(v) == "table" then -- we can iterate over tables
  17.         for l,_ in pairs(v) do
  18.             print(l..": "..v[l])
  19.         end
  20.     elseif type(t[k]) == "function" then -- but not over functions
  21.         print("source code isn't available")
  22.     end
  23.     print("---")
  24. end
Which should output something similar to:
Code:
someTable
table: 0x04C3E578
table: 0x04C3E578
alpha: tableEntry 1
beta: tableEntry 2
---
someFunction
function: 0x04E518F8
function: 0x04E518F8
source code isn't available
---
Also, you don't strictly need two variables to pairs(), but it's considered good practice to replace the unneeded variable with underscore. The following three snippets do the same thing:
Lua Code:
  1. for k,v in pairs(t) do
  2.     t[k] = nil
  3. end
  4.  
  5. for k in pairs(t) do
  6.     t[k] = nil
  7. end
  8.  
  9. for k,_ in pairs(t) do
  10.     t[k] = nil
  11. end
It's the same for any function; you don't have to store all their return values - you'll get as many from the start of the list of return values as you declare variables for, and the rest will be ignored. In most cases it's proper to replace the unneeded return values with underscore, but in other cases that just makes for unreadable code.

For instance, GetUnitBuffInfo() returns ten values, but if I'm only interested in the name of the buff (which is first on the list of return values), it's perfectly fine to use it like this:
Lua Code:
  1. local buffName = GetUnitBuffInfo("player", 1)
instead of the slightly ridiculous
Lua Code:
  1. local buffName, _, _, _, _, _, _, _, _, _ = GetUnitBuffInfo("player", 1)
or even more ridiculous naming nine variables I won't ever use.

It gets a bit messier if I'm only interested in the last return value though...

Last edited by stjobe : 05/12/14 at 06:08 PM. Reason: Expanded pairs argument a bit.
  Reply With Quote