I present DictConstruct, which can create and populate a dictionary in the same call.
// DictConstruct
// Constructs a dictionary and populates it using the string arguments given to it.
// Pass: args - strings of the form "key:value"
// Returns: A dictionary handle populated with the key-to-value mappings you supply.
int DictConstruct(... args)
{
int i;
int dict = DictNew();
string k, v;
for(i = 0; i < args.length; i++)
{
if(args.is_string[i])
{
k = GetToken(args.string[i], ":", 0);
v = TokenRight(args.string[i], ":", 1);
DictSetString(dict, k, v);
}
else
{
Exit("DictConstruct: Argument " + str(i) + " must be a string value.");
}
}
return dict;
}
Here is an example usage:
int dict = DictConstruct("name:Bob", "job:Fighter", "attack:54");