1

I am currently learning LUA and trying to make a script for a game. So far everything works fine, but i stuck at a problem and i cant figure out how to do this.

For example i have a LUA file with this content:

DATA["1234"] = {"Admin", 0, 0}
DATA["2793"] = {"Supporter", 0, 0}
DATA["1599"] = {"VIP", 0, 0}
DATA["6473"] = {"User", 0, 0}
  • DATA = Tablename
  • ["1234"] = Player IDs
  • {"",0,0} = 3 Values for each DATA

What i want is something that give me the number of DATAs i have. In this example i have 4 DATAs. When a new Player comes i have 5 DATAs but how can i get the number of "Keys" or "DATAs" i have?

I need this for a Player-Save function.

EDIT:

I forgot something important, sorry..

I need the Keynames as well, its very important!

This works fine:

function tableLength(T)
    local count = 0
    for _ in pairs(T) do count = count + 1 end
    return count
end

But how can i get the keynames of all DATAs?

Btw: The playerId is the SteamID

Something like:

for k, v in pairs(DATA[playerId]) do
    --Code here
end
-- Results:
1234
2793
1599
6473
Aquedus
  • 13
  • 1
  • 4
  • 1
    you can count them using a `for` loop and `pairs` or you track it with a separate length variable. – Nifim Mar 25 '21 at 18:31
  • 1
    Does this answer your question? [How to get number of entries in a Lua table?](https://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table) – luther Mar 25 '21 at 18:37

2 Answers2

2

The code below prints the keys in DATA:

for k, v in pairs(DATA) do
    print(k)
end
lhf
  • 70,581
  • 9
  • 108
  • 149
0

My suggestion for tables like yours is to installing metamethods that handles exactly your table.
I am using it also for tables, like _G or _ENV where no sequence of numbered keys exists.
Than its easy to count the keys also with #
Example

Lua 5.4.0  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> print(code.len)
-- len() for tables having named keys or keys not in sequence
return function(len)
local incr=0
for _ in pairs(len) do
  incr=incr+1
end
return incr
end
> #_G
0
> setmetatable(_G,{__len=load(code.len)(),__index={}})                                                                                   
table: 0x56616700
> #_G
36
-- Now the dump() function that show all keys/values
> print(code.dump)
-- dump(table)
return function(...)
local args={...}
local test,dump=pcall(assert,args[1])
if test then
for key,value in pairs(dump) do
  io.write(string.format("%s=%s\n",key,value))
end
  return true
else
  return test,dump
end
end
> getmetatable(_G).__index.dump=load(code.dump)()
> _G:dump()
assert=function: 0x565aa300
error=function: 0x565a9be0
ipairs=function: 0x565a9a10
tostring=function: 0x565a9300
next=function: 0x565a9a60
select=function: 0x565a9340
package=table: 0x565d2ee0
os=table: 0x565d3e70
rawlen=function: 0x565a97c0
xpcall=function: 0x565a9e30
print=function: 0x565a9920
string=table: 0x565d45a0
pcall=function: 0x565aa3e0
coroutine=table: 0x565d30e0
arg=table: 0x565d6310
rawset=function: 0x565a9710
dofile=function: 0x565aa280
io=table: 0x565d3750
-- cuted off here
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15