WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Printing table with ["names"] = true, (https://www.wowinterface.com/forums/showthread.php?t=55597)

Mortimerlol 07-25-17 10:25 AM

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!

Rainrider 07-25-17 10:42 AM

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

Mortimerlol 07-25-17 10:55 AM

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...

:D :D

Mortimerlol 07-25-17 10:56 AM

Quote:

Originally Posted by Rainrider (Post 324331)
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 ;)

Mortimerlol 07-25-17 11:58 AM

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);

pas06 07-25-17 12:58 PM

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

Mortimerlol 07-25-17 01:32 PM

Quote:

Originally Posted by pas06 (Post 324338)
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.

pas06 07-25-17 02:13 PM

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);

Mortimerlol 07-26-17 05:56 AM

Quote:

Originally Posted by pas06 (Post 324341)
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.


All times are GMT -6. The time now is 01:00 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI