Coldfusion 测试结构键和数据是否存在的正确语法是什么?
我在使用structKeyExists()测试特定结构键和数据是否存在时遇到问题 我希望我的查询结果存储在如下结构中:Coldfusion 测试结构键和数据是否存在的正确语法是什么?,coldfusion,coldfusion-10,Coldfusion,Coldfusion 10,我在使用structKeyExists()测试特定结构键和数据是否存在时遇到问题 我希望我的查询结果存储在如下结构中: APPLICATION.MemQs.ProdCountQs[1].ExpirationDate; APPLICATION.MemQs.ProdCountQs[1].ProdCount; APPLICATION.MemQs.ProdCountQs[2].ExpirationDate; APPLICATION.MemQs.ProdCountQs[2].ProdCount; APPL
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;
}
if (structKeyExists(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周围加上pound
#
符号,因为它是一个变量
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”]
。因此,代码将如下所示:
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;
}
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”的列
你需要在SomeID周围加上pound##符号,因为它是一个变量。值得注意的是,这只是因为变量名用引号括起来,所以它被视为文本字符串而不是变量名。省略引号,CF将把SomeID
视为变量。此外,虽然使用“==false”有效,但如果(!structKeyExists(APPLICATION.MemQs.ProdCountQs,SomeID)){…}使用“!”ie会更短一些,我在变量周围加了磅号,效果非常好。谢谢structKeyExists(APPLICATION.MemQs.ProdCountQs,#SomeID##“==false)@Leigh。正确的。我正在修改代码以反映它。Pankaj-酷。这将有助于下次出现“引用或不引用问题”;-)Evik-是的,虽然这样做有效,但我会去掉引号和#符号。SomeID
是一个变量(IMO:)这一点打字更少,也更清楚。此外,正确的方法是删除引号,而不是添加磅符号。由于引号是字符串的简写,在删除引号时,实际上是要求CF对照变量的值检查键。