Is there any way to refer to a Table row by one of its variables? Rather than just its ID which could change at any time.
Basically when you insert into a Table, you're throwing stuff in there and the language takes care of assigning it an ID which can be referred to if you call tablename[id].whatever. But you can't always depend on this id being the same.
I know you can assign an ID when you insert it, but I have had problems with this since it seems to hate it if you try to go out of order.
Is there any way to get the id by referring to one of the table rows variable values?
Like MySQL.
If there is, I'd want to give table rows my own personal ID name that would be unique, like an ID on a webpage, that I could then use to refer to it.
Like:
table.insert(tablename, {id = "uniqueID", variable = whatever, another = something})
tablename[rowbyvalue(id = "uniqueID")].variable
Or will I have to write a function to sweep the table, find out what row has the value I am looking for, and return the ID as its result?
I'd hope there was an easier way.
Lua question: Refer to a Table row by name?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Lua question: Refer to a Table row by name?
tables are crazy mutants in lua, they can do anything.
t = {}
t['unique'] = 7
t.unique = 7
both do the same thing, despite being different syntax.
Simialrly
t[17] = 6 --17th item is now a 6
table.insert(t,17,6) -- table now has a 6 at position 17
pretty much the same
if you want to iterate over items in a list by their names, go liek this:
t = {['dog'] = 7, ['row'] = 'row'}
t['bird'] = 'free!'
for name,item in pairs(t) do--not ipairs()
do something
end
names would be 'dog' 'row' and 'bird', items would be 7,'row' and 'free'
t = {}
t['unique'] = 7
t.unique = 7
both do the same thing, despite being different syntax.
Simialrly
t[17] = 6 --17th item is now a 6
table.insert(t,17,6) -- table now has a 6 at position 17
pretty much the same
if you want to iterate over items in a list by their names, go liek this:
t = {['dog'] = 7, ['row'] = 'row'}
t['bird'] = 'free!'
for name,item in pairs(t) do--not ipairs()
do something
end
names would be 'dog' 'row' and 'bird', items would be 7,'row' and 'free'
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Lua question: Refer to a Table row by name?
You might want to start investing your time in hash tables instead of checking the table 1 by 1.
http://en.wikipedia.org/wiki/Hash_table
http://en.wikipedia.org/wiki/Hash_table
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Lua question: Refer to a Table row by name?
I'm not sure what you mean, but you can use tables as unique keys:Jasoco wrote:Is there any way to get the id by referring to one of the table rows variable values?
Code: Select all
unique1 = {}
unique2 = {}
table = {[unique1] = blah, [unique2] = blah}
Not quite:Almost wrote: t[17] = 6 --17th item is now a 6
table.insert(t,17,6) -- table now has a 6 at position 17
pretty much the same
Code: Select all
t = {1, 2, 3}
t[2] = 999
-- t is now {1, 999, 3}
t = {1, 2, 3}
table.insert(t, 2, 999)
-- t is now {1, 999, 2, 3}
Help us help you: attach a .love.
Re: Lua question: Refer to a Table row by name?
Expanding on TechnoCat's idea: use two hashset's, assuming you want to keep a set of tuples. The first hashset would be your keys, the second would be your values. Use natural ordering to "join" the two for your tuples. This solution will give you constant time access for looking up by a value in a hashtable:TechnoCat wrote:You might want to start investing your time in hash tables instead of checking the table 1 by 1.
http://en.wikipedia.org/wiki/Hash_table
Code: Select all
function load()
keys = {"blueberry",
4,
"jingle"}
values = {
["tasty"] = 1,
["age"] = 2,
["bells"] = 3}
superTable = {["keys"]=keys,["values"]=values}
love.graphics.setFont(love.graphics.newFont(love.default_font, 15))
end
function getKey(value)
local joinIdx = superTable.values[value]
local key = superTable.keys[joinIdx]
return key
end
function draw()
love.graphics.draw("What is tasty? -> " ..getKey("tasty"), 250,300)
love.graphics.draw("How old am I? -> " ..getKey("age"), 250,315)
love.graphics.draw("What do bells do? -> " ..getKey("bells"), 250,330)
end
Re: Lua question: Refer to a Table row by name?
It might be quick and dirty but you can always just use 2 tables. One table to store them by their "id", the other where you can do stuff to it, changing order and stuff.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Lua question: Refer to a Table row by name?
Heh, gonna bump this thread because I decided to figure it out a bit.
So, I had used table.insert to create my rows and ipairs() to run through them, but when trying to insert a row at an index I wanted to specify, it would fail. So I switched from table.insert to addressing the array itself by doing tablename[id] = { param=value, param=value} and that works fine now.
I can refer to a row if I make its id a number, but not if I try to give it a name, because if I try to insert a tablename["word"], the game fails when it gets to the:
Saying "attempt to perform arithmetic on local 'i' (a string value)".
Also, table.getn() no longer returns the number of rows in the table with this method. Is there an alternative to getn() that does?
These are my two problems. The first is not a big deal. The second (getn) is what I need to figure out definitely. Can I not get the length of the table list when I place the rows myself?
item[1]
item[2]
item[3]
Would return 3
item[4]
item[13]
item[223]
Returns 0 for some reason. How can I get it to return 3? (Or the actual row count)
So, I had used table.insert to create my rows and ipairs() to run through them, but when trying to insert a row at an index I wanted to specify, it would fail. So I switched from table.insert to addressing the array itself by doing tablename[id] = { param=value, param=value} and that works fine now.
I can refer to a row if I make its id a number, but not if I try to give it a name, because if I try to insert a tablename["word"], the game fails when it gets to the:
Code: Select all
for i, n in pairs(tablename) do
end
Also, table.getn() no longer returns the number of rows in the table with this method. Is there an alternative to getn() that does?
These are my two problems. The first is not a big deal. The second (getn) is what I need to figure out definitely. Can I not get the length of the table list when I place the rows myself?
item[1]
item[2]
item[3]
Would return 3
item[4]
item[13]
item[223]
Returns 0 for some reason. How can I get it to return 3? (Or the actual row count)
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Lua question: Refer to a Table row by name?
Shoot. It's not working how I wanted it to. Seems my method makes table.remove no longer work because running through the pairs doesn't give the i as the right number.
Wait. I have changed table.remove(tablename, i) to tablename = nil. Will setting a row to nil remove it? Since I have no getn() that works, I can't tell if it's being removed or not. Ugh! Why doesn't getn() work?
Edit: Apparently. Seems if I set it to nil, it gets destroyed. So that's good. I seem to have worked around the getn() problem by simply counting the items as they are created and subtracting whenever one is removed. Storing the total in a global variable. It's a hack, but until I get an official row counter it will have to work.
Wait. I have changed table.remove(tablename, i) to tablename = nil. Will setting a row to nil remove it? Since I have no getn() that works, I can't tell if it's being removed or not. Ugh! Why doesn't getn() work?
Edit: Apparently. Seems if I set it to nil, it gets destroyed. So that's good. I seem to have worked around the getn() problem by simply counting the items as they are created and subtracting whenever one is removed. Storing the total in a global variable. It's a hack, but until I get an official row counter it will have to work.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Lua question: Refer to a Table row by name?
Getn has been deprecated, use the length operator instead #.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Lua question: Refer to a Table row by name?
But getn works still. (For tables created with insert) Will it be removed in the future?
And how would I use the length operator?
And how would I use the length operator?
Who is online
Users browsing this forum: Google [Bot] and 6 guests