Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is #table returning 0..!?!

Asked by 8 years ago
local Hair = {
    ['Male'] = {
        ['Trecky'] = {['ID'] = 32278814, ['DecalID'] = 0000000};
        ['Bald'] = {['ID'] = nil, ['DecalID'] = 0000000};
        ['Politician Hair'] = {['ID'] = 12270145, ['DecalID'] = 000000};
        ['Gray Hair'] = {['ID'] = 12864800, ['DecalID'] = 000000};
        ['Messy Hair'] = {['ID'] = 26658141, ['DecalID'] = 000000}
    };
    ['Female'] = {
        ['Golden Hair'] = {['ID'] = 13476917, ['DecalID'] = 000000};
        ['Black and Red Hair'] = {['ID'] = 14815761, ['DecalID'] = 0000000};
        ['Cinnamon Hair'] = {['ID'] = 13745548, ['DecalID'] = 0000000};
        ['Sassy Headband'] = {['ID'] = 25517576, ['DecalID'] = 0000000}
    }
}

So this is my table, I need to grab the number of values in each gender.

However:

print(#Hair['Male'])

Always returns 0... (and the same with female)

Does the #table method not work in this situation?

And if so, is there a better way than using a loop and counting each value?

2 answers

Log in to vote
2
Answered by 8 years ago

Dictionaries have no length.

Without iterating or storing the size manually, there is no way to get the length of a dictionary. Sorry m8

0
Meh, thanks anyway. darkelementallord 686 — 8y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Think about it: what would an answer other than 0 tell you?

Sure you could know "how many" things there are, but that's not particularly helpful. There is no "1st", and you can't guess any of the keys.

Imagine you had a named list, e.g., {1, 5, 3, 8, name = "Bob"}. An answer of 5 for #tab would be misleading at best.

# only counts how many things there are at list[1], list[2], list[3], ... list[#list]. That lets you enumerate those things as for i = 1, #list do. Counting anything else would be misleading.

Answer this question