Thread Tools Display Modes
07-25-17, 10:25 AM   #1
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Question Printing table with ["names"] = true,

Hello, I have the following table in my savedvariable:
Lua Code:
  1. BangCharHateds = {
  2.     ["Demotatz-Quel'Thalas"] = true,
  3.     ["Evies"] = true,
  4.     ["Sengøku-Ysondre"] = true,
  5.     ["Eyman"] = true,
  6.     ["Atheanis"] = true,
  7.     ["Vedepe"] = true,
  8. }
I need print the names in order (A to Z).... Im trying with:
Lua Code:
  1. for key, value in pairs(BangCharHateds) do
  2. if BangCharHateds[key] then
  3. print(BangCharHateds[value])
  4. end
  5. end
But prints true,true,true,true.... I need prints the names

Thanks for help!
  Reply With Quote
07-25-17, 10:42 AM   #2
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
First, you need to get the keys of your BangCharHateds table into an array, then sort the array and then print it.

lua Code:
  1. local toSort = {}
  2. for k in pairs(BangCharHateds) do
  3.     toSort[#toSort + 1] = k
  4. end
  5.  
  6. table.sort(toSort)
  7.  
  8. -- toSort is now sorted
  9. for i = 1, #toSort do
  10.     print(toSort[i])
  11. end
  Reply With Quote
07-25-17, 10:55 AM   #3
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Thanks u so much!! Im going to test your code.

I trying with it...
Lua Code:
  1. -- SORT
  2. local BangCharHatedsOrder = { }
  3.  
  4. for k in pairs(BangCharHateds) do
  5.     tinsert(BangCharHatedsOrder, k)
  6. end
  7. table.sort(BangCharHatedsOrder)
  8.  
  9. for i = 1, #BangCharHatedsOrder do
  10.      local BangCharHatedsOrderedNames = BangCharHatedsOrder[i]
  11.      local categoryData = categories[BangCharHatedsOrderedNames]     
  12.      print(BangCharHatedsOrderedNames)
  13. end
and works...

  Reply With Quote
07-25-17, 10:56 AM   #4
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Originally Posted by Rainrider View Post
First, you need to get the keys of your BangCharHateds table into an array, then sort the array and then print it.

lua Code:
  1. local toSort = {}
  2. for k in pairs(BangCharHateds) do
  3.     toSort[#toSort + 1] = k
  4. end
  5.  
  6. table.sort(toSort)
  7.  
  8. -- toSort is now sorted
  9. for i = 1, #toSort do
  10.     print(toSort[i])
  11. end
Your code likes more
  Reply With Quote
07-25-17, 11:58 AM   #5
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Trying making DropDownMenu

I have the error called "stack overflow"

Lua Code:
  1. local function HSelector(self)
  2.     local toSort = {}
  3.    
  4.     for k in pairs(BangCharHateds) do
  5.         toSort[#toSort + 1] = k
  6.     end
  7. table.sort(toSort)
  8.     for i = 1, #toSort do
  9.         local info = UIDropDownMenu_CreateInfo();
  10.         info.text = toSort[i]
  11.         info.value = toSort[i]
  12.         info.func = function() end;
  13.         UIDropDownMenu_AddButton(info);
  14.     end
  15. end
  16.  
  17. local function UIDropDownMenu_Initialize(btn)
  18.     UIDropDownMenu_Initialize(btn, initialize)
  19.     UIDropDownMenu_SetWidth(btn, 120)
  20.     UIDropDownMenu_SetButtonWidth(btn, 124)
  21.     UIDropDownMenu_SetSelectedID(btn, 1)
  22.     UIDropDownMenu_JustifyText(btn, "LEFT")
  23.  end
  24.  
  25. local dropdown = CreateFrame("Frame", "HSelector", UIParent, "UIDropDownMenuTemplate");
  26. dropdown:SetPoint("CENTER", 0, 0)
  27. dropdown:Show()
  28. UIDropDownMenu_Initialize(dropdown, HSelector);
  Reply With Quote
07-25-17, 12:58 PM   #6
pas06
A Theradrim Guardian
Join Date: Apr 2009
Posts: 62
i never used the function UIDropDownMenu_Initialize (which is an global function of WoW's Api), but you are basically overwriting this global function (not really overwriting globally but for this file) with your own version (local function UIDropDownMenu_Initialize(btn)) in that function you are calling the exact same function again, which creates an infinite loop which leads to a stack overflow
  Reply With Quote
07-25-17, 01:32 PM   #7
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Originally Posted by pas06 View Post
i never used the function UIDropDownMenu_Initialize (which is an global function of WoW's Api), but you are basically overwriting this global function (not really overwriting globally but for this file) with your own version (local function UIDropDownMenu_Initialize(btn)) in that function you are calling the exact same function again, which creates an infinite loop which leads to a stack overflow
ohhhh yes
Lua Code:
  1. local function UIDropDownMenu_Initialize(btn)
  2.   --  UIDropDownMenu_Initialize(btn, initialize)
  3.     UIDropDownMenu_SetWidth(btn, 300)
  4.     UIDropDownMenu_SetButtonWidth(btn, 124)
  5.     UIDropDownMenu_SetSelectedID(btn, 1)
  6.     UIDropDownMenu_JustifyText(btn, "LEFT")
  7.  end
Now the overflow is fixed ^^ but dont generate the list yet.
  Reply With Quote
07-25-17, 02:13 PM   #8
pas06
A Theradrim Guardian
Join Date: Apr 2009
Posts: 62
As i said i am not familiar with that function but what about doing this:
Lua Code:
  1. local function HSelector(self)
  2.     local toSort = {}
  3.    
  4.     for k in pairs(BangCharHateds) do
  5.         toSort[#toSort + 1] = k
  6.     end
  7. table.sort(toSort)
  8.     for i = 1, #toSort do
  9.         local info = UIDropDownMenu_CreateInfo();
  10.         info.text = toSort[i]
  11.         info.value = toSort[i]
  12.         info.func = function() end;
  13.         UIDropDownMenu_AddButton(info);
  14.     end
  15. end
  16.  
  17. local function Initialize_UIDropDownMenu(btn)
  18.     UIDropDownMenu_Initialize(btn, HSelector)
  19.     UIDropDownMenu_SetWidth(btn, 120)
  20.     UIDropDownMenu_SetButtonWidth(btn, 124)
  21.     UIDropDownMenu_SetSelectedID(btn, 1)
  22.     UIDropDownMenu_JustifyText(btn, "LEFT")
  23.  end
  24.  
  25. local dropdown = CreateFrame("Frame", "HSelector", UIParent, "UIDropDownMenuTemplate");
  26. dropdown:SetPoint("CENTER", 0, 0)
  27. dropdown:Show()
  28. Initialize_UIDropDownMenu(dropdown);
  Reply With Quote
07-26-17, 05:56 AM   #9
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Originally Posted by pas06 View Post
As i said i am not familiar with that function but what about doing this:
Lua Code:
  1. local function HSelector(self)
  2.     local toSort = {}
  3.    
  4.     for k in pairs(BangCharHateds) do
  5.         toSort[#toSort + 1] = k
  6.     end
  7. table.sort(toSort)
  8.     for i = 1, #toSort do
  9.         local info = UIDropDownMenu_CreateInfo();
  10.         info.text = toSort[i]
  11.         info.value = toSort[i]
  12.         info.func = function() end;
  13.         UIDropDownMenu_AddButton(info);
  14.     end
  15. end
  16.  
  17. local function Initialize_UIDropDownMenu(btn)
  18.     UIDropDownMenu_Initialize(btn, HSelector)
  19.     UIDropDownMenu_SetWidth(btn, 120)
  20.     UIDropDownMenu_SetButtonWidth(btn, 124)
  21.     UIDropDownMenu_SetSelectedID(btn, 1)
  22.     UIDropDownMenu_JustifyText(btn, "LEFT")
  23.  end
  24.  
  25. local dropdown = CreateFrame("Frame", "HSelector", UIParent, "UIDropDownMenuTemplate");
  26. dropdown:SetPoint("CENTER", 0, 0)
  27. dropdown:Show()
  28. Initialize_UIDropDownMenu(dropdown);
Create a menu selector with the database names.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Printing table with ["names"] = true,

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