find out, how many arrays an array includes

Hi there,

i have an array with arrays in it like this:
 

local x = {

    array1 = {

        var1 = 5

    },

   array2 = {

        var2 = 7

   }

}

so how to find out the number of arrays  in array “x” ?

I tried like this: print(#x)

but it says me always “0”.

Hope someone can help

Thanks in advance :slight_smile:

only works if your table is used like a traditional array, with numeric indices.

when used like a hash-table (with “keys”, as you’re doing) you’ll have to iterate, fe

local count=0

for _ in pairs(x) do count=count+1 end

print(count)

Hi davebollinger,

thanks for your answer.
I already solved the problem on the same way you said.
But I was in hope that there is an easy way like “#” xD.

cheers

only works if your table is used like a traditional array, with numeric indices.

when used like a hash-table (with “keys”, as you’re doing) you’ll have to iterate, fe

local count=0

for _ in pairs(x) do count=count+1 end

print(count)

Hi davebollinger,

thanks for your answer.
I already solved the problem on the same way you said.
But I was in hope that there is an easy way like “#” xD.

cheers