Skip to main content
2Charlie
Inspiring
October 24, 2016
Answered

Why am I getting an error on struct concatenation?

  • October 24, 2016
  • 3 replies
  • 1522 views

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.

    This topic has been closed for replies.
    Correct answer BKBK

    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!


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

    3 replies

    Inspiring
    October 31, 2016

    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.

    BKBK
    Community Expert
    Community Expert
    October 31, 2016

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

    2Charlie
    2CharlieAuthor
    Inspiring
    November 7, 2016

    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>

    Legend
    October 25, 2016

    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.

    BKBK
    Community Expert
    Community Expert
    October 24, 2016

    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.

    2Charlie
    2CharlieAuthor
    Inspiring
    October 25, 2016

    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.

    BKBK
    Community Expert
    Community Expert
    October 26, 2016

    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>