• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Why am I getting an error on struct concatenation?

Enthusiast ,
Oct 24, 2016 Oct 24, 2016

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.

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 26, 2016 Oct 26, 2016

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

...

Votes

Translate

Translate
Community Expert ,
Oct 24, 2016 Oct 24, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 25, 2016 Oct 25, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2016 Oct 25, 2016

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 26, 2016 Oct 26, 2016

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2016 Oct 26, 2016

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">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 25, 2016 Oct 25, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 30, 2016 Oct 30, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 30, 2016 Oct 30, 2016

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) />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2016 Nov 07, 2016

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2016 Nov 07, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

Could you show us the code that defines cfData? It seems cfData is not a struct.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2016 Nov 07, 2016

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) />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2016 Nov 07, 2016

Copy link to clipboard

Copied

LATEST

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) />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation