0


Does lua have a function which returns the lenght of a table?

flag offensive
asked 2010-04-27 14:16:01.933657
1
add comment
5 Answers:
1


Yes, there is the length operator:

#table

And the table.maxn function:

table.maxn(table)
permanent link | flag offensive
answered 2010-04-27 19:27:02.039886
21
add comment
0


Also, an interesting observation, the built-in # operator cannot determine the length of a table or a subset of a table that is used as a dictionary. For example:

#{1,2,3,a=4,5}

will yield 4. notice that elements appended after a key-val pair will be indexed after the last known continuous index

Just as well

example = {1,2,3,4,5}
table.remove(example, 3)
#example

will yield 4 as the elements 4 and 5 are automatically shifted.

permanent link | flag offensive
answered 2010-04-27 20:35:35.596598
41
add comment
0


That's how to get length that includes whole table elements.

function length(Table)
    local Length,a,b = 0
    for a,b in pairs(Table) do
        Length = Length + 1
    end
    return(Length)
end

my_table = { [21] = "ea", ["dsa"] = 21}
print(length(my_table))
permanent link | flag offensive
answered 2010-04-28 18:02:02.303182
1
add comment
0


couldn't resst, but lenght is spelled length lawl

permanent link | flag offensive
answered 2010-04-28 18:47:04.026591
11
add comment
0


Lol, you can say only that? It was spelled right in half post thought. I'm sick at the moment and someone is telling me about spelling mistake in the script variable.

permanent link | flag offensive
answered 2010-04-28 18:55:13.215820
1
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview



Tags:

× 3
× 1

Asked: 2 years ago

Seen: 1,102 times

Last updated: 2 years ago

Made with Django.