I originally was using math.random(1, #table), but I think more or less #table tells you the next available “slot” within the table. This doesn’t really work in my instance because I purposely have empty slots within my table. Is there a way to count the total number of slots within a table? Preferably a count not including the empty slots, but even if someone knows how to count every slot (filled in or empty) it would be a big help. Thanks for any help!
Random chooser for a sparse or non-integer indexed table:
local testTable = {} testTable[1] = 100 testTable[3] = 12 testTable[42] = "The answer" testTable.name = "bob" testTable.corona = "cool" local function getRandomEntry( tbl ) local tmp = {} for k,v in pairs(tbl) do tmp[#tmp+1] = v end local index = math.random(1,#tmp) return tmp[index], index -- return value and index OR --return tmp[index] -- just value end local function getCount( tbl ) local count = 0 for k,v in pairs(tbl) do if( v ) then count = count + 1 end end return count end print(getRandomEntry(testTable)) print(getRandomEntry(testTable)) print(getRandomEntry(testTable)) print(getCount(testTable))
table.maxn() will get you the total number of members, but its going to count the empty spaces too.
Rob
Wow, I knew about for k,v in pairs () do… lines but that’s pretty ingenious how you were able to use it to get rid of the empty slots – I never would have thought of that! The only lines I’m hazy on are the “return tmp[index], index” or only "return tmp[index]. Do the return statements kind of just reset the value to how it was before you put your table into this function?
Hi. I was giving you a couple of options:
-- A. Use this return statement to return the value and its index: return tmp[index], index --Lua can return muliple values from a function -- Example: local function multi() return 1, 2, 3 end local a,b,c = multi() print(a,b,c) -- prints 1 2 3 -- B. The other option was to use this return statement instead and only return the value return tmp[index]
FYI
The function I provided does not modify the original table.
Random chooser for a sparse or non-integer indexed table:
local testTable = {} testTable[1] = 100 testTable[3] = 12 testTable[42] = "The answer" testTable.name = "bob" testTable.corona = "cool" local function getRandomEntry( tbl ) local tmp = {} for k,v in pairs(tbl) do tmp[#tmp+1] = v end local index = math.random(1,#tmp) return tmp[index], index -- return value and index OR --return tmp[index] -- just value end local function getCount( tbl ) local count = 0 for k,v in pairs(tbl) do if( v ) then count = count + 1 end end return count end print(getRandomEntry(testTable)) print(getRandomEntry(testTable)) print(getRandomEntry(testTable)) print(getCount(testTable))
table.maxn() will get you the total number of members, but its going to count the empty spaces too.
Rob
Wow, I knew about for k,v in pairs () do… lines but that’s pretty ingenious how you were able to use it to get rid of the empty slots – I never would have thought of that! The only lines I’m hazy on are the “return tmp[index], index” or only "return tmp[index]. Do the return statements kind of just reset the value to how it was before you put your table into this function?
Hi. I was giving you a couple of options:
-- A. Use this return statement to return the value and its index: return tmp[index], index --Lua can return muliple values from a function -- Example: local function multi() return 1, 2, 3 end local a,b,c = multi() print(a,b,c) -- prints 1 2 3 -- B. The other option was to use this return statement instead and only return the value return tmp[index]
FYI
The function I provided does not modify the original table.