PDA

View Full Version : tables are not simple! or are they?


antonio borraccino
06-14-2007, 09:23 AM
Greetings to all,

Maybe I'm overlooking the obvious here but I can't manage to do something which I think I should be able to do with tables. I'm newby so please forgive my naivety. I've the following table:

tab_def={}
emp01={name="Carlos Freeman", dob="23/12/74", jobTitle="dispatch rider", hobbies="mountain claiming"};
tab_def[emp01]=emp01;
emp02={name="Emma Dobson", dob="02/02/68", jobTitle="dispatch rider", hobbies="cycling"};
tab_def[emp02]=emp02;
emp03={name="Katia Korsacova", dob="04/07/69", jobTitle="post room", hobbies="reading"};
tab_def[emp03]=emp03;

which I add to a listbox:

for k in tab_def do
ListBox.AddItem("list_peo", tab_def[k].name);
end


I would like to be able to select one person from the list and display all info in a single paragraph. All I manage is to dislay the name.

result = ListBox.GetSelected("list_peo");
which_name = ListBox.GetItemText("list_peo", result[1]);

Paragraph.SetText("list_det", which_name);

How can I move forward with this? I'd be grateful for any help.

antonio

bule
06-14-2007, 10:39 AM
Do the list adding like this:

for index,item in tab_def do
ListBox.AddItem("list_peo", item.name);
end

bule
06-14-2007, 10:46 AM
To display details:

result = ListBox.GetSelected("list_peo");
which_name = ListBox.GetItemText("list_peo", result[1]);
for index,item in tab_def do
if item.name=which_name then
details=item.name.."\r\n"..item.dob.."\r\n"..item.jobTitle.."\r\n"..item.hobbies
break
end

Paragraph.SetText("list_det", details);

There are other ways to do it better but this will give you a general idea.

Lorne
06-14-2007, 12:06 PM
I'd do a table of tables. Put the details into a table, and assign that to the main table using the name as the key. Then you don't have to loop to look up the data, just retrieve it associatively.

Example:

-- set up the employee tables
tbEmployees={};
tbEmployees["Carlos Freeman"] = {dob="23/12/74", jobTitle="dispatch rider", hobbies="mountain claiming"};
tbEmployees["Emma Dobson"] = {dob="02/02/68", jobTitle="dispatch rider", hobbies="cycling"};
tbEmployees["Katia Korsacova"] = {dob="04/07/69", jobTitle="post room", hobbies="reading"};

-- empty the list box
ListBox.DeleteItem("list_peo", LB_ALLITEMS);

-- add the employee names to the list box
for name,tbDetails in tbEmployees do
ListBox.AddItem("list_peo", name);
end

-- display the selected employee's information in a paragraph object
-- (put this in the list box object's On Select event)
tbSelected = ListBox.GetSelected("list_peo");
if tbSelected then
name = ListBox.GetItemText("list_peo", tbSelected[1]);
tbDetails = tbEmployees[name];
if tbDetails then
Paragraph.SetText("list_det", name.."\r\n"..tbDetails.dob.."\r\n"..tbDetails.jobTitle.."\r\n"..tbDetails.hobbies);
end
end

antonio borraccino
06-14-2007, 01:43 PM
...but only after someone else shows you how to do it. Joking aside it is very encouraging to be put on the right track within a very short space of time.

Thank you Bule and Lorne.