1

I want a table like the following:

local Users = {}
local function GetUsers (user)
--cycle through all Users

  local Id = GetUserID (user)
  local Age = GetAge (user)
  local Type = GetType (user)
  --Id returns ID of User (value about 8   char length: 27296654)
  table.insert (Users, {[Id] = {Age = Age, Type = Type}}
end

This is working as it should but

#Users == 0

if I call Users[Id].Age it returns correct value.

How to make the # work? As I want to cycle trough all Users to check if a User is multiple times in or missing. They need to be sorted via IDS. I also thought on transforming the IDs to word with string.char() As words will be counted as I want it to be.

I want to make it with

for i = 1, #Users do

An example table looks like this:

Users = {
  [12345678] = {Age = 18, Type = 1}
  [62952766] = {Age = 22, Type = 1}
  [23456788] = {Age = 33, Type = 1}
}

So #Users have to be 3 for me but it shows 0. But I can call the Age and type out of the table, that means they are in. Does the table have Problems when the index is such a high number?

TheMaxxxel
  • 11
  • 2

1 Answers1

4

Look at # operator documentation:

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

You are using dictionary, so the # operator doesn't work as you expect it. The only way is to iterate the whole table with pairs(..).

function getTableLength(T)
    local count = 0
    for _ in pairs(T) do 
        count = count + 1 
    end
    return count
end
warspyking
  • 3,045
  • 4
  • 20
  • 37
Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56
  • Thanks, gonna try it when I'm home. So when I transform the ID to a word (f.e. Id [23456789] = id [abfhsgzt]) also don't work the way I want? – TheMaxxxel Nov 19 '15 at 07:33
  • because I want to call the values in the table with Users [n] where n = 1, #Users. Or I there any other way to create a table with Users that are not twice in there? I tried many things but they always stack up like 1, 3, 7, 378, 292901, out of memory. – TheMaxxxel Nov 19 '15 at 07:46
  • No, the # should work as you expected only in regular array. This means that indexs is trictly one by one from 1 no n (1,2,3,4...n). – Nick Bondarenko Nov 19 '15 at 09:46
  • And please look at this http://stackoverflow.com/questions/6618843/lua-smartest-way-to-add-to-table-only-if-not-already-in-table-or-remove-duplic question. In simple - just use Users[Id] = {Age = Age, Type = Type} – Nick Bondarenko Nov 19 '15 at 09:56
  • This answer is correct but would be better using the terminology of the current version of Lua. They replace the term "regular array" with the more descriptive term "sequence." – Tom Blodget Nov 20 '15 at 00:09