PDA

View Full Version : How to programmatically set elements of associative array


Ken Gartner
04-18-2005, 05:35 AM
I want to create/modify items in an associative table. The names of the properties are not known at design time, but are read from a file. I do not see an API such as Table.Add(propertyName, propertyValue) and I have not had much success finding an alternative. Any suggestions about how to accomplish this?

Example:

When TU runs, it downloads a file listing my patch definitions. Each patch might include script-specific flags. I have a common patch property set library that will pass this info along to each script. The script will unpack the properties and use them. I was hoping to set the properties in a local associative array to avoid polluting the global namespace.



scriptVariables="CreateRestorePoint=true;FileRoot=files/patch1234";

local MyVariables = {};

while (true) do
local nFoundPos = String.Find(searchString, ";");
if (nFoundPos ~= -1) then
local propertySetting = String.Left(searchString, nFoundPos -1);
local nEqualPos = String.Find(propertySetting, "=");
if (nEqualPos ~= -1) then
local PropertyName = String.Left(propertySetting, nEqualPos -1);
local PropertyValue = String.Right(propertySetting, String.Length(propertySetting) - nEqualPos);
// The following line is syntactically incorrect, but I want the effect myVariables.CreateRestorePoint = true
myVariables...PropertyName = PropertyValue;
end
searchString = String.Right(searchString, String.Length(searchString) - nFoundPos);
else
break;
end
end

Thanks for your suggestions.

Ken Gartner

Mark
04-18-2005, 08:17 AM
Hi Ken,

What you should do is use the [] brackets in order to accomplish the addition to the table.

So substitute your questionable line below with the following:

MyVariables[PropertyName] = PropertyValue;

This will basically add an entry into the MyVariables table at position PropertyName, with value ProperyValue.