How to dynamically loop through an array of structures

I need to create a request object from an array of structures. The array has about 200 keys, and the structures within the array vary in name, size, and depth. This is what node looks like:

array of structures I need to loop thru and create query object

I am already working on array [#i] .adGroupId and array [#i #]. userStatus, but I need to check if the structural value exists before adding it to the request. For example, the .text criterion is not always in the structure, so I need to check this, but I don't know how. Here's the code so far, but it always skips criterion.text and bids.maxCpc.amount.microAmount.

<cfset vColumns = "AdGroupID,KeywordText,Status,MaxCPC" />
<cfset vColumnValue = "AdGroupID,criterion.text,userStatus,bids.maxCPC" />
<cfset loopnum = 1>
<cfset myquery = QueryNew(vColumns) >
<cfloop array="#aKeywordsAll#" index="i">
    <cfset temp = QueryAddRow(myquery, 1)>
    <cfset loopNum2 = 1>
    <cfloop list="#vColumnValue#" index="j">                
        <cfif structKeyExists(aKeywordsAll[loopnum],j)>
            <cfset QuerySetCell(myquery, listGetAt(vColumns, loopNum2), aKeywordsAll[loopnum][j])>
        <cfelse>
            <cfset QuerySetCell(myquery, listGetAt(vColumns, loopNum2), "test")>
        </cfif>
        <cfset loopNum2++ />
    </cfloop>
    <cfset loopnum++ />
</cfloop>

      

The request object has been created here. He says "test" but hopes that he will give the values:

Query object created from my crazy looping

+2


source to share


1 answer


Your problem is that the StructKeyExists function is looking for a key literally called "criterion.text" (for example) - this is possible, not a "text" key inside a key called "criterion". I hacked UDF which should solve the problem for you:

 <cffunction name="StructGetByKeyList">
    <cfargument name="struct">
    <cfargument name="key">

    <cfset var result = "">

    <cfif StructKeyExists(struct,ListFirst(key,"."))>
        <cfif ListLen(key,".") GT 1>
            <cfreturn StructGetByKeyList(struct[ListFirst(key,".")],ListRest(key,"."))>
        <cfelse>
            <cfreturn struct[key]>
        </cfif>
    <cfelse>
        <cfreturn "">
    </cfif>
 </cffunction>

      

Then you can call Len (StructGetByKeyList (aKeywordsAll [loopnum], j)) instead of structKeyExists (aKeywordsAll [loopnum], j).

For this line:



 <cfset QuerySetCell(myquery, listGetAt(vColumns, loopNum2), aKeywordsAll[loopnum][j])>

      

Use this:

 <cfset QuerySetCell(myquery, listGetAt(vColumns, loopNum2), StructGetByKeyList(aKeywordsAll[loopnum],j))>

      

+5


source







All Articles