Array size

Asked

Viewed 946 times

4

How do I get the size of an array with this structure:

A = {
    ["b"] = {c=1, d=2},
    ["e"] = {f=1, g=2},
}

I want to take the size of A, in this case, 2.

  • That one link should help.

  • 1

    What you need that size for?

  • 1

    @lhf so you need this question?

  • 1

    @Bacco, because I never needed to know the size of a table. I’m trying to know what real problem the OP is trying to solve.

  • 2

    @lhf So, but it seems that Gabriel needs hehehe. I just asked the question in the same tone to demonstrate the initial strangeness that the question causes. But it is good to know that the "father of matter" has an eye on the Moon issues here with us ;) As for Gabriel’s question, I suppose it is related to this one: http://answall.com/questions/31288/

1 answer

6


Lua has tables and no arrays. Lua tables have semantics of array and semantics of hash depending on how you place your elements, and this can be a little confusing.

A array must have a sequential integer index preferably starting at 1. The size is defined by the number of elements. If really is a array and start at 1, we know that the highest index corresponds to the table size. But what you’re trying to verify in the example is the size of a hashs and not a array.

There are some possible solutions depending on your goal.

table.getn(A)

or

#A

This is the most obvious solution but can bring different results than you expect if you have any element nil in the middle of the table. It counts the amount of elements until you find a nil. The second is the operator of length. If you can guarantee that you have one array and not a hash this might work.

table.maxn(A)

This form returns the highest possible index in the table. It can be the solution you want depending on the constitution of the table. If it does not contain null elements in the table this is confused with the total of elements. It works better for tables that are hashs. It is not the focus of the title of your question but it is what you are effectively using. You have primarily a hash and not a array. It is good to understand the difference.

function table.map_length(t)
    local c = 0
    for k,v in pairs(t) do
         c = c+1
    end
    return c
end

This algorithm counts all elements in a table that may have arbitrary indexes. It also works well for hashs. These solutions can be more guaranteed if you are not absolutely sure that you have a array at hand. Source.

Test with your example in the ideone. I do not put examples of others because they will not return the expected result to your example.

function table.map_length(t)
    local c = 0
    for k,v in pairs(t) do
         c = c+1
    end
    return c
end
 
A = {
["b"] = {c=1, d=2},
["e"] = {f=1, g=2},
}
 
print(table.map_length(A))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    Perfect, thank you, I’ll study further

Browser other questions tagged

You are not signed in. Login or sign up in order to post.