WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to get element count for this table? (https://www.wowinterface.com/forums/showthread.php?t=56315)

alikim 06-30-18 08:39 AM

How to get element count for this table?
 
Code:

PRF = {
        ["Profile_1"] = {
                ["pve"] = {
                        {
                                "Blood Frenzy", -- [1]
                                22420, -- [2]
                        }, -- [1]
                        {
                                "Guttural Roars", -- [1]
                                22424, -- [2]
                        }, -- [2]
                        {
                                "Balance Affinity", -- [1]
                                22163, -- [2]
                        }, -- [3]
                        {
                                "Mass Entanglement", -- [1]
                                18576, -- [2]
                        }, -- [4]
                        {
                                "Incarnation: Guardian of Ursoc", -- [1]
                                21706, -- [2]
                        }, -- [5]
                        {
                                "Survival of the Fittest", -- [1]
                                22422, -- [2]
                        }, -- [6]
                        {
                                "Pulverize", -- [1]
                                22425, -- [2]
                        }, -- [7]
                },
                ["pvp"] = {
                },
                ["bar"] = {
                },
        },
}

How do I get number of profile records in this table?

The function below gives me "count 0 0", I expect 1 for "Profile_1"

Code:

print("count " .. table.getn(PRF) .. " " .. #PRF)
Thank you!

LanceDH 06-30-18 09:17 AM

getn only works with positive integer keys, from 1 until the sequence breaks.
If the key is another type, such as a table or string in this case, it won't count it.
Lua Code:
  1. local t = {"a", "b", "c"}
  2. print(#t); -- prints 3
  3. t[5] = "e";
  4. print(#t); -- still prints 3, because there is no key 4
  5. t["4"] = "d";
  6. print(#t); -- still prints 3, because key "4" is a string, not an integer

To get the number of all keys in a table, you have to create a for loop:
Lua Code:
  1. local count = 0;
  2. for key, value in pairs(PRF) do
  3.    count = count + 1;
  4. end
  5. print(count); -- will print 1
Note that I used pairs for this for loop instead of ipairs. It's the same thing where ipairs will only loop the sequencing integer keys (1-3 in table t from the first example)

alikim 06-30-18 10:04 AM

T h a n k s !


All times are GMT -6. The time now is 07:08 AM.

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