Quantcast
Help comparison tables - WoWInterface
Thread Tools Display Modes
07-31-17, 07:58 AM   #1
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Exclamation Help comparison tables

I have this code:
Lua Code:
  1. local BangDB = CreateFrame("FRAME");
  2. BangDB:RegisterEvent("ADDON_LOADED");
  3. BangDB:RegisterEvent("PLAYER_ENTERING_WORLD");
  4.  
  5. local function eventHandler(self, event, ...)
  6.     if event == "ADDON_LOADED" and ... == "Bang_Bang" then
  7.         if not BangCharKills then BangCharKills = {} end
  8.         if not BangCharHateds then BangCharHateds = {} end
  9.  
  10.         self:UnregisterEvent("ADDON_LOADED");
  11.     end
  12.  
  13. -- SORT TABLE A BY NAMES - format{ ["Name"] = true, ["Nametwo"] = true, ... }
  14. local hates = {}
  15.     for k in pairs(BangCharHateds) do hates[#hates + 1] = k end
  16.         table.sort(hates)
  17.     for i = 1, #hates do print(hates[i]) end
  18.  
  19. -- SORT TABLE B BY KILLS, NAMES - format{ ["Name"] = 12, ["Nametwo"] = 5, ... }
  20. function spairs(BangCharKills, order)
  21.     local tokills = {}
  22.     for k in pairs(BangCharKills) do tokills[#tokills + 1] = k end
  23.     if order then
  24.         table.sort(tokills, function(a,b) return order(BangCharKills, a, b) end)
  25.     else
  26.         table.sort(tokills)
  27.     end
  28.     local i = 0
  29.     return function()
  30.         i = i + 1
  31.         --testing with:
  32.         --if tokills[i] == hates[i] then -- doesn't works
  33.         --if BangCharKills[tokills[i]] == hates[i] then -- doesn't works
  34.         if tokills[i] then
  35.             return tokills[i], BangCharKills[tokills[i]]
  36.         end
  37.     end
  38. end
  39.     for k,v in spairs(BangCharKills, function(BangCharKills,a,b) return BangCharKills[b] < BangCharKills[a] end) do
  40.         print(v, k)
  41.     end
  42. end
  43. BangDB:SetScript("OnEvent", eventHandler);
All works fine but now I need sort TABLE B with (number, name) but if name is in TABLE A...

Thanks for help!
  Reply With Quote
08-03-17, 07:52 AM   #2
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
I can't get a positive code, somebody have an idea?
  Reply With Quote
08-03-17, 03:25 PM   #3
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Not sure if it is me but I have no clue what you really want to do. Could you explain better, I guess this is the reason you got no answers yet.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill
  Reply With Quote
08-03-17, 07:29 PM   #4
briskman3000
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 75
I'm not 100% sure either.

My best guess would be that he wants to sort the values in Table B, by whether or not the value in [NAME] in Table B exists in [NAME] in Table A.
  Reply With Quote
08-03-17, 10:25 PM   #5
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Sry for my bad english.

My code prints sort table b by values, now i need sort same but if name exists in table a, table a dont have numeric values (have true or false).
  Reply With Quote
08-04-17, 01:40 AM   #6
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Code:
local tableC = {}
for k,v in pairs(tableB)
	if tableA[v] then
		tableC[k] = v
	end
end
table.sort(tableC)
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 08-04-17 at 01:42 AM.
  Reply With Quote
08-04-17, 06:00 AM   #7
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Originally Posted by Banknorris View Post
Code:
local tableC = {}
for k,v in pairs(tableB)
	if tableA[v] then
		tableC[k] = v
	end
end
table.sort(tableC)
Thanks for reply!

I trying with ur code and cant be good.
Lua Code:
  1. local tableC = {}
  2. for k,v in pairs(BangCharKills) do tableC[#tableC + 1] = k end
  3.     if hates[v] then
  4.         tableC[k] = v
  5.     end
  6. table.sort(tableC)
  7.     --for i = 1, #tableC do print(tableC[i]) end
  8.     print(tableC[1])
  9. end
  Reply With Quote
08-04-17, 12:28 PM   #8
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Sorry for my english. I go to explain in code text, i hope that now you understand me:

Lua Code:
  1. BangCharHateds = {
  2.     ["Paco"] = true,
  3.     ["Anabel"] = true,
  4.     ["Francisco"] = true,
  5.  }
  6.   BangCharKills = {
  7.     ["Tarmanu"] = 43,
  8.     ["Anabel"] = 56,
  9.     ["Armario"] = 23,
  10.     ["Paco"] = 10,
  11.     ["Francisco"] = 11,
  12.     ["Casiopea"] = 22,
  13.     ["Artamundo"] = 16,
  14.     ["Costera"] = 7,
  15.  }
  16.  
  17. --  OUTPUT:
  18.  
  19. --  56 Anabel
  20. --  11 Francisco
  21. --  10 Paco

Thanks for help !!!!!!!!
  Reply With Quote
08-04-17, 01:15 PM   #9
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
Originally Posted by Mortimerlol View Post
Thanks for reply!

I trying with ur code and cant be good.
Lua Code:
  1. local tableC = {}
  2. for k,v in pairs(BangCharKills) do tableC[#tableC + 1] = k end
  3.     if hates[v] then
  4.         tableC[k] = v
  5.     end
  6. table.sort(tableC)
  7.     --for i = 1, #tableC do print(tableC[i]) end
  8.     print(tableC[1])
  9. end
Thanks for providing input and expected output.
I don't understand how did you derive your code from example of Banknorris.
Why
Code:
tableC[#tableC + 1] = k end
in line 2 is required? Why there's "end" in line 9 instead of being after line 5?
You'll probably want to use your specialized sorting instead of table.sort.
Do you use notepad for writing, or some dedicated script editor (semi-random indentation doesn't help)?
  Reply With Quote
08-04-17, 01:21 PM   #10
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Originally Posted by Kakjens View Post
Thanks for providing input and expected output.
I don't understand how did you derive your code from example of Banknorris.
Why
Code:
tableC[#tableC + 1] = k end
in line 2 is required? Why there's "end" in line 9 instead of being after line 5?
You'll probably want to use your specialized sorting instead of table.sort.
Do you use notepad for writing, or some dedicated script editor (semi-random indentation doesn't help)?
I was testing, bad copy/paste. Yes, i use Notepad++...
  Reply With Quote
08-06-17, 12:36 AM   #11
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Need it only to close my addon, some suggestion? THANKS!
  Reply With Quote
08-06-17, 04:32 PM   #12
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 931
I have no idea what you're trying to do
  Reply With Quote
08-06-17, 08:31 PM   #13
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Lua Code:
  1. local BangCharHateds = {
  2.     ["Paco"] = true,
  3.     ["Anabel"] = true,
  4.     ["Francisco"] = true,
  5. }
  6.  
  7. local BangCharKills = {
  8.     ["Tarmanu"] = 43,
  9.     ["Anabel"] = 56,
  10.     ["Armario"] = 23,
  11.     ["Paco"] = 10,
  12.     ["Francisco"] = 11,
  13.     ["Casiopea"] = 22,
  14.     ["Artamundo"] = 16,
  15.     ["Costera"] = 7,
  16. }
  17.  
  18. local names = {}
  19. local numbers = {}
  20.  
  21. for char_name,number_of_kills in pairs(BangCharKills) do
  22.     if BangCharHateds[char_name] then
  23.         if not names[number_of_kills] then
  24.             numbers[#numbers+1] = number_of_kills
  25.             names[number_of_kills] = {char_name}
  26.         else
  27.             names[number_of_kills][#names[number_of_kills]+1] = char_name
  28.         end
  29.     end
  30. end
  31.  
  32. table.sort(numbers)
  33. for i=#numbers,1,-1 do
  34.     table.sort(names[numbers[i]])
  35.     for j=1,#names[numbers[i]] do
  36.         print(numbers[i],names[numbers[i]][j])
  37.     end
  38. end
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill
  Reply With Quote
08-07-17, 12:05 AM   #14
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Ohhhhhhhhh excelent work
Thanks u so much Banknorris
  Reply With Quote
08-07-17, 12:33 AM   #15
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Reading from my saved variables not limit to total names compared and repeat names... if find 4 names do:

numbers1 names1
numbers2 names2
numbers3 names3
numbers4 names4
numbers1 names1
numbers2 names2
numbers3 names3
numbers4 names4
...the result is fine but very repeat.

And I cant do:
Lua Code:
  1. print("TOP 1: "..numbers[1].." - "..names[1])
  2. print("TOP 2: "..numbers[2].." - "..names[2])
  3. print("TOP 3: "..numbers[3].." - "..names[3])

Thanks u all one more time
  Reply With Quote
08-07-17, 04:07 AM   #16
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
In provided code I don't see the reason for repeated printing.
You can add a variable to count how many times you have printed and with the help of it exit the loops.
P.S. I am crying.
  Reply With Quote
08-07-17, 05:11 AM   #17
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Ok, I was doing bad. Now not do loop...
Lua Code:
  1. function hTops()
  2.     local names = {}
  3.     local numbers = {}
  4.      
  5.     for char_name,number_of_kills in pairs(BangKills) do
  6.         if BangHateds[char_name] then
  7.             if not names[number_of_kills] then
  8.                 numbers[#numbers+1] = number_of_kills
  9.                 names[number_of_kills] = {char_name}
  10.             else
  11.                 names[number_of_kills][#names[number_of_kills]+1] = char_name
  12.             end
  13.         end
  14.     end
  15.      
  16.     table.sort(numbers)
  17.     for i=#numbers,1,-1 do
  18.         table.sort(names[numbers[i]])
  19.  
  20.         for j=1,#names[numbers[i]] do
  21.           --  print(numbers[1],names[numbers[1]][1])
  22.         print(numbers[i],names[numbers[i]][j])
  23.            
  24.         end
  25.     end
  26. end
Now trying print line per line.
  Reply With Quote
08-07-17, 06:49 AM   #18
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
[solved]

Well, now is done!

Lua Code:
  1. table.sort(numbers, function(a,b)
  2.     return b < a
  3. end)
  4.  
  5. for i=1,1, -1 do
  6.     for j=1,1 do
  7.         --foreach(numbers, print)
  8.         print("TOP 1: "..numbers[1], names[numbers[1]][j])
  9.         print("TOP 2: "..numbers[2], names[numbers[2]][j])
  10.         print("TOP 3: "..numbers[3], names[numbers[3]][j])
  11.     end
  12. end

Thanks Banknorris for help me.
  Reply With Quote
08-07-17, 07:01 AM   #19
pas06
A Theradrim Guardian
Join Date: Apr 2009
Posts: 61
what is this outer for loop good for?
  Reply With Quote
08-07-17, 07:11 AM   #20
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
pas06, both for loops are strange.
Mortimerlol, make Paco, Anabel and Francisco have the same number of kills, and test what happens.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help comparison tables

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