PDA

View Full Version : How do I address a table whose name I know only as a variable?


JimS
08-24-2005, 06:37 AM
I have created a number of tables that contain the names of the counties, for each state of the US. I’ve named the tables CountiesAL, CountiesAK, etc. I’ve added these tables to my globals.

Here is what I want to happen, you pick a State from ComboBox1. Say you pick Alabama the Data for Alabama is AL (selected_data = “AL”)

Now I want to automatically fill ComboBox2 with the proper counties from that State, drawn from the table called CountiesAL

I get the table name by concatenating “Counties”..selected_data

The problem is when I try to query the table, instead of a table, it sees just the string.

My question is how do I address a table whose name I know only as a variable (string)?

Here is a sample of the code I’m trying


-- Gets the index of the selected item.
selected_index = ComboBox.GetSelected("ComboBox1");

-- Make sure an item is selected.
if (selected_index ~= -1) then

-- Reset The ComboBox of County names
ComboBox.ResetContent("ComboBox2");


selected_data = ComboBox.GetItemData("ComboBox1", selected_index);

for j, k in ("Counties"..selected_data ) do
result = ComboBox.AddItem("ComboBox2", k);
end

end

JimS
08-24-2005, 07:28 AM
OK, I’m sure that I’m overlooking the obvious, and I’m probably making this much harder than it needs to be, but I have figured out a way to make it work.

I dynamically write out a little Lua script file, then run it.


-- Gets the index of the selected item.
selected_index = ComboBox.GetSelected("ComboBox1");

-- Make sure an item is selected.
if (selected_index ~= -1) then

--Reset ComboBox of counties
ComboBox.ResetContent("ComboBox2");

--Get selected State Abbreviation
selected_data = ComboBox.GetItemData("ComboBox1", selected_index);

-- Write Lua script file
TextFile.WriteFromString("AutoPlay\\Docs\\my.lua", "for j, k in Counties"..selected_data.." do \r\nComboBox.AddItem(\"ComboBox2\", k);\r\nend", false);

--Run newly created Lua script
Application.RunScriptFile("AutoPlay\\Docs\\my.lua");


end

TJ_Tigger
08-24-2005, 08:00 AM
I have not done it yet, but what is returned if you do this

Dialog.Message("Type", type("Counties"..selected_data));

or this

tblNewTable = "Countries"..Selected_data;
Dialog.Message("Type", type(tblNewTable));

Tigg

Worm
08-24-2005, 08:12 AM
This is a little different approach, but all in all, the same idea.

JimS
08-24-2005, 08:18 AM
Thanks for looking Tigger. Both of the dialog boxes return string

JimS
08-24-2005, 08:30 AM
Thanks Worm, I’ll need to reformat my tables a little, but it looks like it will work. I appreciate it, I figured that there must be some way to do it without having to write out a file. This way should be quicker, thanks again. :)

Worm
08-24-2005, 08:37 AM
I too have tried to use a table that was "dynamically" named in the past and never found an answer. Although I'm definitely ADD when it comes to stuff like that. I probably spend more time looking for a work-around than it would take to figure it out.