How do I build a lookup table?

I’m trying to build a lookup table in lua so that when a user selects a particular person, I can return that person’s info. Problem is, I can’t seem to figure out how to access a particular person in the table…

[lua] – The following would normally come from a user’s choice.
local selectedDealer = “Gerald”

dealers = {
dealerName = “Gerald”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}
dealers = {
dealerName = “Tom”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}

– The following returns ‘nil’
if table.indexOf( dealers[dealerName], selectedDealer) then
print (“Gerald Found!”)
elseif table.indexOf( dealers[dealerName], selectedDealer) then
print (“Tom Found!”)
else
print (“None found!”)
end[/lua]

Any help would be greatly appreciated!! [import]uid: 64538 topic_id: 33637 reply_id: 333637[/import]

Well initially it looks like you are overwriting your dealers table; the “Tom” entry is overwriting the “Gerald” entry.

If you want multiple entries try either:

 table.insert(dealers,index)  

or

dealers = {{dealerName = "Gerald",  
 mainPhone = "(512) 555-1212",  
 salesPhone = "(512) 555-1212",  
 servicePhone = "(512) 555-1212",  
 },  
 {  
 dealerName = "Tom",  
 mainPhone = "(512) 555-1212",  
 salesPhone = "(512) 555-1212",  
 servicePhone = "(512) 555-1212",  
 }  
 }  

or

dealers[#dealers+1] = { dealerName = "Tom", mainPhone = "(512) 555-1212", salesPhone = "(512) 555-1212", servicePhone = "(512) 555-1212", } [import]uid: 94868 topic_id: 33637 reply_id: 133743[/import]

Try this out

[lua]-- The following would normally come from a user’s choice.
local selectedDealer = “Gerald”

dealers = {{
dealerName = “Gerald”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
},
{
dealerName = “Tom”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}}

for i = 1, #dealers do
if dealers[i].dealerName == selectedDealer then
print("Name: ",dealers[i].dealerName)
print("Main Phone: ",dealers[i].mainPhone)
print("Sales Phone: ",dealers[i].salesPhone)
print("Service Phone: ",dealers[i].servicePhone)
end
end[/lua]

Notice all I really did was make ‘dealers’ one complete table instead of two, and created two tables within ‘dealers’ for each dealer respectively. Then using dealers[i].dealerName you can get any data on that dealer you need. [import]uid: 40731 topic_id: 33637 reply_id: 133744[/import]

As far as I know it shouldn’t be a performance problem, how many arrays (dealers) are you planning on having in the database?

I’m working on a project now that uses two tables each with 64 values in them, I have eight for loops running through in steps of eight each at a time with no slow down.

And besides with my experience on tables in the past I’ve found they can be pretty powerful and efficient on a large scale. [import]uid: 40731 topic_id: 33637 reply_id: 133750[/import]

That looks like it would take a hit on performance though unless I’m mistaking something?

Originally I was trying to use something like the following, but couldn’t figure out how to make that work either…

[lua]local selectedDealer = “Gerald”

dealers.Gerald = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}
dealers.Tom = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}[/lua]

As you can probably tell, I’m just trying to figure out how to create a 2D array, and how to do a quick lookup in that array. [import]uid: 64538 topic_id: 33637 reply_id: 133749[/import]

I found this to work, but seems like there should be an easier way to do the comparison…

[lua] local selectedDealer = “Fred”

dealers = {}

dealers[1] = {
dealerName = “Gerald”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}
dealers[2] = {
dealerName = “Tom”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

– The following returns ‘nil’
if dealers[1].dealerName == selectedDealer then
print (“Gerald Found!”)
elseif dealers[2].dealerName == selectedDealer then
print (“Tom Found!”)
else
print (“None Found!”)
end[/lua] [import]uid: 64538 topic_id: 33637 reply_id: 133752[/import]

or

[code]
dealers = {}

dealers[“Gerald”] = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

dealers[“Tom”] = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

if dealers[dealerName] then
print (dealerName … " Found!")
else
print (dealerName…" Not Found!")
end
[/code] [import]uid: 94868 topic_id: 33637 reply_id: 133780[/import]

Screaming Leaf has it correct. Build your table with the dealer names as the keys, and other associated info for that dealer in the key’s unique table. Lua will do a very fast lookup this way, and the structure allows you to add, edit, or delete info about any dealer by looking up his/her key, then specifying the desired sub-key for that dealer.

Brent [import]uid: 200026 topic_id: 33637 reply_id: 133808[/import]

Thanks, that looks like exactly what I needed! [import]uid: 64538 topic_id: 33637 reply_id: 133858[/import]

Well initially it looks like you are overwriting your dealers table; the “Tom” entry is overwriting the “Gerald” entry.

If you want multiple entries try either:

 table.insert(dealers,index)  

or

dealers = {{dealerName = "Gerald",  
 mainPhone = "(512) 555-1212",  
 salesPhone = "(512) 555-1212",  
 servicePhone = "(512) 555-1212",  
 },  
 {  
 dealerName = "Tom",  
 mainPhone = "(512) 555-1212",  
 salesPhone = "(512) 555-1212",  
 servicePhone = "(512) 555-1212",  
 }  
 }  

or

dealers[#dealers+1] = { dealerName = "Tom", mainPhone = "(512) 555-1212", salesPhone = "(512) 555-1212", servicePhone = "(512) 555-1212", } [import]uid: 94868 topic_id: 33637 reply_id: 133743[/import]

Try this out

[lua]-- The following would normally come from a user’s choice.
local selectedDealer = “Gerald”

dealers = {{
dealerName = “Gerald”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
},
{
dealerName = “Tom”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}}

for i = 1, #dealers do
if dealers[i].dealerName == selectedDealer then
print("Name: ",dealers[i].dealerName)
print("Main Phone: ",dealers[i].mainPhone)
print("Sales Phone: ",dealers[i].salesPhone)
print("Service Phone: ",dealers[i].servicePhone)
end
end[/lua]

Notice all I really did was make ‘dealers’ one complete table instead of two, and created two tables within ‘dealers’ for each dealer respectively. Then using dealers[i].dealerName you can get any data on that dealer you need. [import]uid: 40731 topic_id: 33637 reply_id: 133744[/import]

As far as I know it shouldn’t be a performance problem, how many arrays (dealers) are you planning on having in the database?

I’m working on a project now that uses two tables each with 64 values in them, I have eight for loops running through in steps of eight each at a time with no slow down.

And besides with my experience on tables in the past I’ve found they can be pretty powerful and efficient on a large scale. [import]uid: 40731 topic_id: 33637 reply_id: 133750[/import]

That looks like it would take a hit on performance though unless I’m mistaking something?

Originally I was trying to use something like the following, but couldn’t figure out how to make that work either…

[lua]local selectedDealer = “Gerald”

dealers.Gerald = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}
dealers.Tom = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”,
}[/lua]

As you can probably tell, I’m just trying to figure out how to create a 2D array, and how to do a quick lookup in that array. [import]uid: 64538 topic_id: 33637 reply_id: 133749[/import]

I found this to work, but seems like there should be an easier way to do the comparison…

[lua] local selectedDealer = “Fred”

dealers = {}

dealers[1] = {
dealerName = “Gerald”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}
dealers[2] = {
dealerName = “Tom”,
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

– The following returns ‘nil’
if dealers[1].dealerName == selectedDealer then
print (“Gerald Found!”)
elseif dealers[2].dealerName == selectedDealer then
print (“Tom Found!”)
else
print (“None Found!”)
end[/lua] [import]uid: 64538 topic_id: 33637 reply_id: 133752[/import]

or

[code]
dealers = {}

dealers[“Gerald”] = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

dealers[“Tom”] = {
mainPhone = “(512) 555-1212”,
salesPhone = “(512) 555-1212”,
servicePhone = “(512) 555-1212”
}

if dealers[dealerName] then
print (dealerName … " Found!")
else
print (dealerName…" Not Found!")
end
[/code] [import]uid: 94868 topic_id: 33637 reply_id: 133780[/import]

Screaming Leaf has it correct. Build your table with the dealer names as the keys, and other associated info for that dealer in the key’s unique table. Lua will do a very fast lookup this way, and the structure allows you to add, edit, or delete info about any dealer by looking up his/her key, then specifying the desired sub-key for that dealer.

Brent [import]uid: 200026 topic_id: 33637 reply_id: 133808[/import]

Thanks, that looks like exactly what I needed! [import]uid: 64538 topic_id: 33637 reply_id: 133858[/import]