0

Hi I need to create a table in lua with each entry(record) can be represented by unique id

table[p1d2].seq={0,1,2,3} table[p1d2].days={'sun','mon','wed'}
table[p2d2].seq={0,1,2,3,4} table[p2d2].days={'fri','sat','tue'}

print(table.concat(table[p1d2].seq))==> 0123

like that i want to insert and access please help me in solving this riddle

Abhi
  • 73
  • 8
  • Use `table["p1d2"]` instead of `table[p1d2]` – Egor Skriptunoff Jan 06 '21 at 13:29
  • I want to store and access using uid, if uid==p1d2 then print( table[uid].seq) should give 0,1,2,3 if uid=p2d2 print(table[uid].days) should give "fri, sat, tue" like that, I mean store every record(num,days etc...) using a unique id (i.e uid) which will be useful to me while accessing please help me in building this messy logic – Abhi Jan 06 '21 at 14:12
  • Use `if uid=="p1d2" then print( table[uid].seq)` instead of `if uid==p1d2 then print( table[uid].seq)`. Or you have already variable `p1d2` initialized with some value? – Egor Skriptunoff Jan 06 '21 at 14:48

2 Answers2

0

I'm not sure to understand what you're asking for, since tables in Lua natively handle string indexes, for example the following snippet:

tab = {};
tab['one']=1;
print(tab['one']);

prints 1...if that is not what you're asking, could you please try to explain more precisely what behavior you want?

And if that was your question, a code like this one should work (I defined a Thing class since I see you seem to use a class with seq and days fields, but I guess you already have something of the kind in your code, so I only left it for informational purposes).

-- class definition
Thing = {seq = {}, days ={}}

function Thing:new (o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

-- definition of the table and IDs

tab={}
p1d2='p1d2'
p2d2='p2d2'

-- this corresponds to the code you gave in your question

tab[p1d2]=Thing:new()
tab[p2d2]=Thing:new()
tab[p1d2].seq={0,1,2,3}
tab[p1d2].days={'sun','mon','wed'}
tab[p2d2].seq={0,1,2,3,4}
tab[p2d2].days={'fri','sat','tue'}

print(table.concat(tab[p1d2].seq))
-- prints '0123'
  • Hi actually the index(uid) should be variable(it should be unique) like tab={ }; tab[uid].num={} tab[uid].num={2,3,4,5} where uid="p1d1",or uid="p2d1" or uid='p1d2' etc... while accessing when I want to print tab[p1d1].num it should print corresponding uid "num" array only if I want tab[p2d2].num I can able to access that corresponding 'uid' array only (I think I've poorly framed the question now I hope you've an Idea) – Abhi Jan 06 '21 at 13:43
  • I want to store and access using uid, if uid==p1d2 then print( table[uid].seq) should give 0,1,2,3 if uid=p2d2 print(table[uid].days) should give "fri, sat, tue" like that, I mean store every record(num,days etc...) using a unique id (i.e uid) which will be useful to me while accessing please help me in building this messy logic – Abhi Jan 06 '21 at 14:12
  • that p1d2 should not be limited up to p2d2 it should be extended up to pndn (my intension is that p1d2 should be dynamically generated one) we need to assign records (days,seq) dynamically and access like table[uid].seq when uid==p1d2 should give corresponding value uid should be variable – Abhi Jan 06 '21 at 14:21
0

If you want to create table entries with a unique id why not simply use numeric table keys?

local list = {}

table.insert(list, {seq={0,1,2,3}, days={"sun", "mon", "wed"}})
table.insert(list, {seq={0,1,2,3,4}, days= {'fri','sat','tue'}})

That way each entry added will have a unique id automatically without having to generate one. You can then use that index to address the entry later.

If that is not what you're asking for you should provide more detail and examples

Piglet
  • 27,501
  • 3
  • 20
  • 43