我遇到了使用structKeyExists()测试特定结构键和数据是否存在的问题。
我希望我的查询结果存储在以下结构中:
APPLICATION.MemQs.ProdCountQs[1].ExpirationDate;
APPLICATION.MemQs.ProdCountQs[1].ProdCount;
APPLICATION.MemQs.ProdCountQs[2].ExpirationDate;
APPLICATION.MemQs.ProdCountQs[2].ProdCount;
APPLICATION.MemQs.ProdCountQs[3].ExpirationDate;
APPLICATION.MemQs.ProdCountQs[3].ProdCount;
// CREATE MEMORY QUERIES ~ works great
if (structKeyExists(APPLICATION, "MemQs") == false) {
APPLICATION.MemQs = structNew();
}
// CREATE PRODUCT COUNT QUERIES ~ works great
if (structKeyExists(APPLICATION.MemQs, "ProdCountQs") == false) {
APPLICATION.MemQs.ProdCountQs= structNew();
}
// fails the test ~ always recreates variables
if (structKeyExists(APPLICATION.MemQs, "ProdCountQs[SomeID]") == false) {
APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}
// fails the test ~ always recreates variables
if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "[SomeID]") == false) {
APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}
// fails the test ~ always recreates variables
if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "SomeID") == false) {
APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}
// throws error
// APPLICATION.MemQs.ProdCountQs[SomeID], must be a syntactically valid variable name
if (isDefined("APPLICATION.MemQs.ProdCountQs[SomeID]") == false) {
APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}
那么,我如何使用structKeyExists()来测试是否存在
APPLICATION.MemQs.ProdCountQs[SomeID]
答案 0 :(得分:1)
你需要在##
周围加上SomeID
符号,因为它是一个变量。
if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "#SomeID#") == false) {
APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}
如果SomeID
不是变量,那么在这种情况下,您应该使用双引号SomeID
(如""
的方括号括起APPLICATION.MemQs.ProdCountQs["SomeID"]
。所以代码看起来像这样:
if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "SomeID") == false) {
APPLICATION.MemQs.ProdCountQs["SomeID"] = structNew();
APPLICATION.MemQs.ProdCountQs["SomeID"].ExpirationDate = now();
APPLICATION.MemQs.ProdCountQs["SomeID"].ProdCount = 0;
}
修改强>
Leigh建议最好使用!
运算符,而不是检查false
。此外,如果键是变量,那么您可以在检查键时省略双打引号和井号。因此,您可以按如下方式编写if
条件:
if (!structKeyExists(APPLICATION.MemQs.ProdCountQs, SomeID))
答案 1 :(得分:1)
您似乎正在尝试从查询中创建结构。您应首先创建结构,然后从查询中填充它。在下面的示例中,我假设您有一个查询对象,并且该查询对象中的列名为" ExpirationDate"和" ProdCount"。
// create query structure
if(!structKeyExists(APPLICATION, "MemQs")){
APPLICATION.MemQs = structNew();
APPLICATION.MemQs.ProdCountQs = arrayNew();
}
// loop through the query
for(row=1;row<=originalQuery.recordcount;row++){
APPLICATION.MemQs.ProdCountQs[row] = structNew();
APPLICATION.MemQs.ProdCountQs[row].ExpirationDate = originalQuery.ExpirationDate[row];
APPLICATION.MemQs.ProdCountQs[row].ProdCount = originalQuery.ProdCount[row];
}
但是,如果你想要的是检查节点是否存在,你应该像这样递增地遍历树:
// create query structure
if(!structKeyExists(APPLICATION, "MemQs")){
APPLICATION.MemQs = structNew();
if(!structKeyExists(APPLICATION.MemQs, "ProdCountQs")){
APPLICATION.MemQs.ProdCountQs = arrayNew();
if(!arrayIsDefined(APPLICATION.MemQs.ProdCountQs,someID)){
APPLICATION.MemQs.ProdCountQs[someID] = structNew();
if(!structKeyExists(APPLICATION.MemQs.ProdCountQs[someID], "ExpirationDate")){
APPLICATION.MemQs.ProdCountQs[someID].ExpirationDate = now();
}
if(!structKeyExists(APPLICATION.MemQs.ProdCountQs[someID], "ProdCount")){
APPLICATION.MemQs.ProdCountQs[someID].ProdCount = 0;
}
}
}
}