Pairs in Table not returning keys
Greetings, fellow ESOUI brethren.
It seems I am having trouble attempting to empty out a nested table structure in my addon and I was wondering if anyone else may have encountered the same problem as I. Apparently, there's no way to delete or unassign a table, but I can set all the elements to nil. However, iterating over pairs isn't working. When I ask for pairs: Code:
for k in pairs (table) do Code:
/script d(table) Code:
.(function): GetInterfaceForCharacter = function: 9F3AA8E0 Does anyone know how to fix this issue? I'd greatly appreciate it! V/R, -Raven |
Hey Raven,
In modern versions of lua, garbage collection is automagically done. However, for optimal performance, you may want to run it manually. I hope this is what you're looking for. Quote:
http://www.lua.org/manual/5.1/manual...collectgarbage Cheers! |
Quote:
Lua Code:
|
I always use two parameters for a pairs loop. Give that a try. Can't think right now and have to run, there would be another way to do it if that won't work.
Oh - and DON'T force the garbage collector to run. :p |
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:
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:
Code:
someTable Lua 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:
Lua Code:
It gets a bit messier if I'm only interested in the last return value though... ;) |
If that code fragment is exactly the same and not just an example, then ditch the table name of "table". Use something else. Table is a special "word" in LUA.
Also, if you decide to use two parameters for pairs/ipairs/next, take advantage of it. All of these below are equivalent: Code:
for k in next, renamedTable do |
Quote:
You could wrap it in {} to get a table and index it from there: Lua Code:
|
Quote:
The correct way (which I just learned and want to share) of getting the last value from GetUnitBuffInfo() is using select(): Lua Code:
Quote:
Lua Code:
Code:
1: alfa |
Quote:
------ Clearing out a table still leaves an empty table behind, which you can reuse. Depending on the size of your table and how often this is done, you should weigh the pros and cons of clearing and reusing a table as compared with just creating a new table and letting the garbage collector pick up the old one on the next pass. |
Okay, I kinda found a work-around, but...
So, with using Zgoo, I was able to find out some interesting information.
When you grab SavedVariables with: Code:
myVar = ZO_SavedVars:NewAccountWide("mySavedVars", ... ) So while I had "myTable" that I was working with trying to clean out and failing miserably, it's because everything I've thrown at it hasn't been able to affect the reference in any way. I can't even get keys from it because it only returns the meta information. But, when doing the same thing with myAddon_SavedVars.myTable["@Username"]["$AccountWide"], it works. Pairs works, and nil assignment to clear the table out, everything works. However, it's a really crappy way to do business. Does anyone know how to force the table to be dereferenced? Or am I stuck in using this workaround? |
Ah, the fact that you're trying to work with saved variables is a crucial bit of information that you hadn't previously mentioned.
If you're trying to completely clear out SVs, then you'll want to work with the raw variable. |
Pretty much everyone uses that workaround. I just go the longer way, and manually set old, unused stuff to nil so that I can see what I've done in the past. On wishlist, I wished for a command/function that'd clear the saved variables.
|
Quote:
-- Sorry I didn't explicitly state it (although it is in the manual I linked), but all references to data must be dereferenced before the memory is freed. |
Generally, the point is you don't need to run the garbage collector manually because it will be cleaned up as part of the normal routine of the Lua system. Forcing a GC cycle means you affect the entire system as it sets aside cycles to do a complete sweep, something it already has scheduled for in the near future.
Essentially, unless you are creating thousands of tables and niling them all out in one fell swoop, the gains from forcing a GC pale against the hit it gives to performance overall. It's unnecessary optimisation, your discarded tables will be cleaned up with the next GC cycle along with everything else. And given that this whole situation is a once off application (ie, you're clearing the tables once and not doing it again), manual GC is unnecessary. |
Quote:
Aaaalso your link and examples do not explain nor state anywhere that the garbage collector SHOULD be run manually, only that it CAN. And just because you can do something, does not mean you should. Forcing the GC to run in any programming is an optimization technique that should be avoided unless it can't. In the world of add-on development, I can't think of any case in which the GC would ever NEED to be run, except in the most ridiculously complicated add-ons (which itself might have its own issues). *also, Wobin is awesome. |
Quote:
I'll add your gold star to my collection! Thanks! :) |
All times are GMT -6. The time now is 04:48 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI