Jump to content

Getting lengh of a non-indexed table


'LinKin

Recommended Posts

When working with tables like this way:

tableName["someString"] = Something

If you use #tableName, it would return 0. Always.

Is there any other method to return the table's lengh than making a function for it? Just like:

function tableLengh()

local count = 0

for i, k in pairs(tableName) do

count = count +1

end

return count

end

Link to comment

It depends on how you define your table, see this example in how you can use the # symbol to get the length.

local myTable = { 
    [1]="something1", 
    [2]="something2", 
    [3]="something3"  
} 
  
local myTable2 = {} 
myTable2["something"] = somethingElse 
  
outputChatBox("Length of table1: "..tostring(#myTable)) 
outputChatBox("Length of table2: "..tostring(#myTable2)) 

Link to comment
  • Moderators
It depends on how you define your table, see this example in how you can use the # symbol to get the length.
local myTable = { 
    [1]="something1", 
    [2]="something2", 
    [3]="something3"  
} 
  
local myTable2 = {} 
myTable2["something"] = somethingElse 
  
outputChatBox("Length of table1: "..tostring(#myTable)) 
outputChatBox("Length of table2: "..tostring(#myTable2)) 

@MrBrutus

He doesn't use the array so # becomes useless.

His table is something like this:

local myTable = { 
    ["cow_I_love"]=1, 
    ["cow_I_ hate"]=2, 
    ["cow_I_kiss(so sexy)" ]=3 
} 

Link to comment
  • Moderators
Are you sure? couldn't though it was such a difference in using strings or integers as keys. I'm not an expert in lua tables but maybe this could be helpful: http://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table. Someone that appears to have the same issue there.

yes,

His question was if there was another way then the pairs loop.

Afaik there is one other way using meta tables. (which I do not understand and not sure if this is possible)

Link to comment
Afaik there is one other way using meta tables. (which I do not understand and not sure if this is possible)

Yes, it is. Here's some quick code I wrote:

local tablecount_mt = { 
    __n = 0, 
    __data = {}, 
    __index = function ( t, k ) 
  
        local metatable = getmetatable ( t ) 
  
        if k == "__count" then 
            return metatable.__n 
        end 
  
        return metatable.__data[k] 
  
    end, 
    __newindex = function ( t, k, v ) 
  
        local metatable = getmetatable ( t ) 
  
        if metatable.__data[k] then 
            if v == nil then 
                metatable.__n = metatable.__n - 1 
            else 
                metatable.__n = metatable.__n + 1 
            end 
        else 
            metatable.__n = metatable.__n + 1 
        end 
  
        metatable.__data[k] = v 
     
    end 
} 
  
local myt = {} 
setmetatable ( myt, tablecount_mt ) 
  
myt["hi"] = "world" -- myt.__count == 1 
myt["world"] = "hello" -- myt.__count == 2 
myt["test1"] = "hi" -- myt.__count == 3 
myt["world"] = nil -- myt.__count == 2 
myt["hi"] = nil -- myt.__count == 1 
myt["test2"] = 2 -- myt.__count == 2 
print ( myt["test2"] ) 

It stores the table data in another table within the metatable, so __newindex is called for entries that were already added, which makes it possible to tell when an index was set to nil. Basically, what this code does is when you attempt to add a new index or change an index, it checks if the data already exists, if it does: it decrements the table size by one if the new value is nil, or increments by one if it isn't. If the data doesn't exist yet, it just increments the table size by one. Then, it stores the new data in the internal table. In the __index metatable event, we check if we're trying to access the index '__count' and returns the internal table size variable if so. Else, it just looks up the key in the internal data table.

Link to comment

__newindex is triggered when one attempts to set the value of an index that doesn't yet exist in the table. __index is triggered when attempting to access the value of an index that doesn't exist in the table. That's why in my code the actual table data is stored internally in the metatable, so that __newindex is triggered every time you try to set the value of an index (since there is no such key in the actual table, it triggers the event).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...