测试结构键和数据是否存在的正确语法是什么?
What is the correct syntax to test whether a structure key and data exists?
我在使用 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]
您需要在 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))
您似乎正在尝试根据查询创建结构。您应该首先创建结构,然后从您的查询中填充它。在下面的示例中,我假设您有一个查询对象,并且该查询对象中有名为 "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;
}
}
}
}
我在使用 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]
您需要在 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))
您似乎正在尝试根据查询创建结构。您应该首先创建结构,然后从您的查询中填充它。在下面的示例中,我假设您有一个查询对象,并且该查询对象中有名为 "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;
}
}
}
}