Thread Tools Display Modes
05-26-10, 10:28 AM   #1
Fodaro
A Cyclonian
 
Fodaro's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 42
Concatenating keys and values of a table, without making a garbage heap

In the last couple of releases of Consolid8, I've been using a StaticPopup to report things to the user. I anchor a FontString to the left for the faction names, and one to the right for the values. To put the text into them, I wrote this piece of code:
lua Code:
  1. local namesStr, valuesStr = "", ""
  2. for key, value in pairs(data) do
  3.     namesStr  = namesStr  .. key   .. "\n"
  4.     valuesStr = valuesStr .. value .. "\n"
  5. end
Thing is, it makes a lot of garbage. It'd be good to use table.concat, but that only uses the array part of a table. Although it's not called that often, I don't like the idea of the garbage that would be made if there was a lot of data. Any ideas?
__________________
Fodaro
(Main: Fodaro-Bronzebeard (EU))
Author of CharNote and Consolid8

Last edited by Fodaro : 05-27-10 at 12:38 PM.
  Reply With Quote
05-26-10, 11:45 AM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
lua Code:
  1. local function concatk(t, k, ...)
  2.     local newk, newv = next(t, k)
  3.    
  4.     if not newv then return ... end
  5.    
  6.     return concatk(t, newk, newk, ...)
  7. end
  8.  
  9. local function concatv(t, k, ...)
  10.     local newk, newv = next(t, k)
  11.    
  12.     if not newv then return ... end
  13.    
  14.     return concatv(t, newk, newv, ...)
  15. end
  16.  
  17.  
  18. strjoin("\n", concatk(data, nil))
  19. strjoin("\n", concatv(data, nil))

Last edited by Slakah : 05-26-10 at 11:47 AM.
  Reply With Quote
05-27-10, 12:38 PM   #3
Fodaro
A Cyclonian
 
Fodaro's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 42
Smile

Thanks for that, it's perfect
__________________
Fodaro
(Main: Fodaro-Bronzebeard (EU))
Author of CharNote and Consolid8
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Concatenating keys and values of a table, without making a garbage heap

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