Copy link to clipboard
Copied
I have the following code and ColdFusion Builder 3 is showing I'm getting an error on line 17 on the cfdata.result & #count# concatenation.
<cfset cfData = structNew() />
<cfloop condition = "count lt numHttpCalls" >
<h4>Head: #count#</h4>
<cfif not ArrayIsEmpty(cateData) >
<cfhttp url="https://example.com/api/head/category.json" method="get" timeout="15" throwonerror="false" proxyServer="webproxy.example.com" proxyport="8070">
<cfhttpparam type="url" name="_authbykey" value="56a7d8c5123465c4058361237">
<cfhttpparam type="url" name="project_id" value="25c4ffd13123456527e294fe6">
<!---The limit has to be less than 10 or elese the snippet will be ignored --->
<cfhttpparam type="url" name="limit" value="10"/>
<!---Specify the categories we want to display on the page below.--->
<cfloop index="x" from="1" to="#arrayLen(cateData)#">
<!---<p>Cate Name: #cateData
.values.url_hash#</p>---> <cfhttpparam type="url" name="url_hash[$in][]" value="#cateData
.values.url_hash#"/> </cfloop>
</cfhttp>
</cfif>
<cfset cfData.result & #count# = DeserializeJSON(cfhttp.FileContent) />
<cfset count=count + 1>
</cfloop>
Any help is much appreciated.
As I showed earlier, you could use 'count' as an index. Have a look at this example:
<cfloop from="1" to="4" index="count">
<cfset dynamicname = '{"employees":[{"firstName":"John", "lastName":"Doe#count#"}]}'>
<cfset cfData.result[count] = DeserializeJSON(dynamicname) >
</cfloop>
<cfdump var="#cfData#" label="cfData before append">
<cfloop from="2" to="4" index="idx">
<cfset structAppend(cfData.result[1],cfData.result[idx],false) >
</cfloop>
<cfdump var="#cfData#" label="cfData after appe
...Copy link to clipboard
Copied
There is no such thing as 'struct concatenation'. Strings, say, 'str A' and 'str B' are simple values and so may be concatenated as 'str A' & 'str B'. The result is 'str Astr B'.
Structs however are complex values which may contain key-value pairs. Concatenation is therefore meaningless here.
But you could do something like
<cfset cfData.result[count] = DeserializeJSON(cfhttp.FileContent) >
Then count is a key of the struct cfData.result and result is a key of the struct cfData.
Copy link to clipboard
Copied
Thanks for the response. Okay, that resolves that issue but when I tried this, it failed. I got a "Error in custom script module".
<cfset structAppend(cfData.result0, cfData.result1, false) />
The variable count starts with 0 and in this case it only goes up to 1.
Copy link to clipboard
Copied
2Charlie wrote:
Thanks for the response. Okay, that resolves that issue but when I tried this, it failed. I got a "Error in custom script module".
<cfset structAppend(cfData.result0, cfData.result1, false) />
The variable count starts with 0 and in this case it only goes up to 1.
The 0 and 1 in the names result0 and result1 are not indices. So count is irrelevant to them.
Structappend applies to 2 structs, so first check if cfData.result0 and cfData.result1 are structs.
<cfif isStruct(cfData.result0) and isStruct(cfData.result1)>
</cfif>
Copy link to clipboard
Copied
BKBK, basically actually what I'mt trying to do is some how structAppend a dynamic numbers of cfData.result. So instead of specifically put down <cfset structAppend(cfData.result0, cfData.result1, cfData.result2, cfData.result3, false) /> and so on, I want to be able to dynamically structAppend them. Is this possible? Thanks!
Copy link to clipboard
Copied
As I showed earlier, you could use 'count' as an index. Have a look at this example:
<cfloop from="1" to="4" index="count">
<cfset dynamicname = '{"employees":[{"firstName":"John", "lastName":"Doe#count#"}]}'>
<cfset cfData.result[count] = DeserializeJSON(dynamicname) >
</cfloop>
<cfdump var="#cfData#" label="cfData before append">
<cfloop from="2" to="4" index="idx">
<cfset structAppend(cfData.result[1],cfData.result[idx],false) >
</cfloop>
<cfdump var="#cfData#" label="cfData after append">
Copy link to clipboard
Copied
My best suggestion is to find a good beginners book on coldfusion. Learning Coldfusion in 21 Days or Coldfusion for Idiots (not an evaluation, just the title) are both good books. Structures are very basic and you need a good grasp of them as well as arrays and simple values to succeed with CF.
Copy link to clipboard
Copied
If you are trying to create a number of variables (like it seems in your original snippet) then try this:
<cfset cfData["result#count#"] = DeserializeJSON(cfhttp.FileContent) />
This would create variables named cfData.result1, cfData.result2, etc.
Copy link to clipboard
Copied
So far, the problem has been about the use of stuctAppend(), not really about the naming convention. (Re: the original question). In any case, another suggestion along the same lines:
<cfset cfData["result" & count] = DeserializeJSON(cfhttp.FileContent) />
Copy link to clipboard
Copied
Okay, I think I've got pass all that but I got an error in line 2 saying "Element RESULT is undefined in a Java object of type class [Ljava.lang.String;"
<cfloop condition="categoryIndex lt count" >
<cfif not ArrayIsEmpty(cfData.result[categoryIndex].data)>
<cfset categoryArray = cfData.result[categoryIndex].data>
<!---Calling function to display the categories and its articles.--->
<cfinvoke method="displayCategories">
<cfinvokeargument name="categories" value="#categoryArray#">
</cfinvoke>
<cfelse>
<p>No data found for <b>#hash#</b>.<p>
</cfif>
<cfset categoryIndex++ >
</cfloop>
Copy link to clipboard
Copied
Okay, I think I got it resolved by using not ArrayIsEmpty(cfData['result#categoryIndex#'].data)
Thank you for all the help.
Copy link to clipboard
Copied
Could you show us the code that defines cfData? It seems cfData is not a struct.
Copy link to clipboard
Copied
This is what I have. Line 2 is inside a loop that the condition is less than the array length.
<cfset cfData = structNew() />
<cfset cfData["result#count#"] = DeserializeJSON(cfhttp.FileContent) />
Copy link to clipboard
Copied
2Charlie wrote:
Okay, I think I got it resolved by using not ArrayIsEmpty(cfData['result#categoryIndex#'].data)
That's fine. Another way to cut it is:
<cfset resultCategoryIndex = "result" & categoryIndex>
<cfif isArray(cfData[resultCategoryIndex].data) and not arrayIsEmpty(cfData[resultCategoryIndex].data)>
<cfset cfData["result#count#"] = DeserializeJSON(cfhttp.FileContent) />
<cfset resultCount = "result" & count />
<cfset cfData[resultCount] = DeserializeJSON(cfhttp.FileContent) />