lua: retrieve list of keys in a table - lua

I need to know how to retrieve the key set of a table in lua. for example, if I have the following table:
tab = {}
tab[1]='a'
tab[2]='b'
tab[5]='e'
I want to be retrieve a table that looks like the following:
keyset = {1,2,5}

local keyset={}
local n=0
for k,v in pairs(tab) do
n=n+1
keyset[n]=k
end
Note that you cannot guarantee any order in keyset. If you want the keys in sorted order, then sort keyset with table.sort(keyset).

local function get_keys(t)
local keys={}
for key,_ in pairs(t) do
table.insert(keys, key)
end
return keys
end

Related

join two string to one table in Lua

I have 2 strings:
fields="a,b,c,d,e"
values="1,2,,4,5"
I need a table, to get the pairs values like:
print(result.a) -> "1"
print(result.c) -> "" (or nil)
Is it possible?
Here is an opportunity to exploit generators without a for loop. The code below runs two gmatch generators in tandem.
fields="a,b,c,d,e"
values="1,2,,4,5"
fields=fields.."," ; F=fields:gmatch("(.-),")
values=values.."," ; V=values:gmatch("(.-),")
result={}
while true do
local k,v=F(),V()
if k==nil or v==nil then break end
result[k]=v
end
for k,v in pairs(result) do print(k,v) end

Doesnt seem to get the reference in lua, but only the value

I am getting Components, which are saved in tables(I use a special system for better memory effiency). Then i give them as parameters in a given function(with the unpack statement(I already checked it isnt the reason)). So far so good, I get the value that is saved in the Table. But if I change it the component in the table doesnt change value. So in short: I want to give the component by reference but I am giving it by value. I thought Lua always gives values stored in tables by reference. Any help would be appreciated. If you need any additional resources just ask :). Thankyou in advance
function pool:run(dt)-- runs all active systems
local sprite = self.img
for i, method in ipairs(self.mPool) do
if method[1] then
--finds entities with required components
local matches = {}
for x=1, #self.ePool do
if band(self.ePool[x][1], method[2]) == method[2] then
matches[#matches+1] = x
end
end
--get components of entities
local components = {}
for x=1, #method[3] do
components[x] = {}
local marker=1
local savePosition = 1
for Eid=1, matches[#matches] do-- Eid = entity id
if Eid == matches[marker] then
components[marker][#components[marker]+1] = self.cPool[method[3][x]][savePosition]
marker = marker +1
end
if self.cBool[method[3][x]][Eid] then
savePosition = savePosition +1
end
end
end
--reorder and run as coroutine or function
if method[5] then
for x=1, #components do
coroutine.wrap(method[4])(matches[x], unpack(components[x]), dt)
end
else
for x=1, #components do
method[4](matches[x], unpack(components[x]), dt)
end
end
end
end
end
The problem is that if you set a variable to the value of table content the value not the reference will be set.

How to create a table of unique strings in Lua?

I'm trying to create a function that adds unique string to a table. I also wonder how to print the result.
My Code :
local t = {}
function addUniqueString(str)
--what should be here?
end
function printElements()
--what should be here?
end
addUniqueString("apple")
addUniqueString("orange")
addUniqueString("banana")
addUniqueString("apple")
printElements()
The Result I want : (order doesn't matter)
apple
orange
banana
Since the order doesn't matter, you can just add strings as keys to the table:
local t = {}
function addUniqueString(str)
t[str] = true
end
And to list the strings:
function printElements()
for k in pairs(t) do
print(k)
end
end

how to make a function version of this

I made a program on Lua to display a table content, it also display a content of a table that is inside this table
for i in pairs(v) do
if type(v[i])=="table" then
for j in pairs(v[i]) do
if type(v[i][j])=="table" then
print("...")
else
print(i,j,v[i][j])
end
end
else
print(i,v[i])
end
end
My question is, is possible to make a version function of the above that work with undefined number of table inside table like {{{1},1},1} showing something like?
1 1 1 1
1 2 1
2 1
You need a recursive function. See the globals example in the Lua online demo, reproduced below. Call dump with your table instead of _G.
-- globals.lua
-- show all global variables
local seen={}
function dump(t,i)
seen[t]=true
local s={}
local n=0
for k in pairs(t) do
n=n+1 s[n]=k
end
table.sort(s)
for k,v in ipairs(s) do
print(i,v)
v=t[v]
if type(v)=="table" and not seen[v] then
dump(v,i.."\t")
end
end
end
dump(_G,"")

Lua, Tables: merge values of duplicate keys and remove duplicates

I've started fiddling a lot with lua recently, but I can't for my life figure this out.
Let's say I have a string that looks like this:
s = "a=x a=y b=z a=x"
I want to remove all duplicates and merge the values of duplicate keys into a table, so that I get:
t = {
a = {x,y},
b = {z},
}
I've been pondering about this for way too long. Any help is appreciated!
Try this:
s="a=x a=y b=z a=x"
s=s.." "
t={}
for k,v in s:gmatch("(.-)=(.-)%s+") do
if t[k]==nil then t[k]={} end
t[k][v]=true
end
for k,v in pairs(t) do
for z in pairs(v) do print(k,z) end
end

Resources