WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Help comparison tables (https://www.wowinterface.com/forums/showthread.php?t=55616)

Mortimerlol 07-31-17 07:58 AM

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!

Mortimerlol 08-03-17 07:52 AM

I can't get a positive code, somebody have an idea?

Banknorris 08-03-17 03:25 PM

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.

briskman3000 08-03-17 07:29 PM

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.

Mortimerlol 08-03-17 10:25 PM

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

Banknorris 08-04-17 01:40 AM

Code:

local tableC = {}
for k,v in pairs(tableB)
        if tableA[v] then
                tableC[k] = v
        end
end
table.sort(tableC)


Mortimerlol 08-04-17 06:00 AM

Quote:

Originally Posted by Banknorris (Post 324485)
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

Mortimerlol 08-04-17 12:28 PM

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

Kakjens 08-04-17 01:15 PM

Quote:

Originally Posted by Mortimerlol (Post 324486)
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)?

Mortimerlol 08-04-17 01:21 PM

Quote:

Originally Posted by Kakjens (Post 324491)
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++...

Mortimerlol 08-06-17 12:36 AM

Need it only to close my addon, some suggestion? THANKS!

Ketho 08-06-17 04:32 PM

I have no idea what you're trying to do

Banknorris 08-06-17 08:31 PM

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

Mortimerlol 08-07-17 12:05 AM

Ohhhhhhhhh excelent work :D :D
Thanks u so much Banknorris :) :) :) :) :)

Mortimerlol 08-07-17 12:33 AM

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 :o

Kakjens 08-07-17 04:07 AM

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.

Mortimerlol 08-07-17 05:11 AM

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.

Mortimerlol 08-07-17 06:49 AM

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

pas06 08-07-17 07:01 AM

what is this outer for loop good for?

Kakjens 08-07-17 07:11 AM

pas06, both for loops are strange.
Mortimerlol, make Paco, Anabel and Francisco have the same number of kills, and test what happens.


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

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