Evik James
Evik James

Reputation: 10473

What is the correct syntax to test whether a structure key and data exists?

I am having an issue testing for the existence of a specific structure key and data using structKeyExists().

I want my query results to be stored in structures like this:

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;
}

So, how do I use structKeyExists() to test for the existence of

APPLICATION.MemQs.ProdCountQs[SomeID]

Upvotes: 2

Views: 129

Answers (2)

Pankaj
Pankaj

Reputation: 1741

You need to put pound ## signs around SomeID as it is a variable.

if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "#SomeID#") == false) {
   APPLICATION.MemQs.ProdCountQs[SomeID] = structNew();
   APPLICATION.MemQs.ProdCountQs[SomeID].ExpirationDate = now();
   APPLICATION.MemQs.ProdCountQs[SomeID].ProdCount = 0;
}

If SomeID is not a variable then in that case you should surround SomeID in square braces with double quotes "" like this APPLICATION.MemQs.ProdCountQs["SomeID"]. So the code will look like this:

if (structKeyExists(APPLICATION.MemQs.ProdCountQs, "SomeID") == false) {
   APPLICATION.MemQs.ProdCountQs["SomeID"] = structNew();
   APPLICATION.MemQs.ProdCountQs["SomeID"].ExpirationDate = now();
   APPLICATION.MemQs.ProdCountQs["SomeID"].ProdCount = 0;
}

EDIT
As Leigh suggested it is better to use ! operator instead of checking for false. Also if the key is a variable then you can omit the doubles quotes and pound sign while checking for the key. So you can write the if condition as follows:

if (!structKeyExists(APPLICATION.MemQs.ProdCountQs, SomeID))

Upvotes: 1

rodmunera
rodmunera

Reputation: 552

Looks like you're trying to create a structure from a query. You should create the structure first and then populate it from your query. In the sample below I'm assuming you have a query object and that there's columns in that query object called "ExpirationDate" and "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];
}

However, if what you want is to check for the existence of the nodes, you should go through the tree incrementally like so:

// 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;
            }
        }
    }
}

Upvotes: 1

Related Questions