Skip to main content
2Charlie
Inspiring
Oct 24, 2016 2:58 PM UTC
Answered

Why am I getting an error on struct concatenation?

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 is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Oct 26, 2016 9:03 PM UTC

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

    13 replies

    Inspiring
    Oct 31, 2016 1:36 AM UTC

    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.

    Translate
    BKBK
    Community Expert
    Community Expert
    Oct 31, 2016 6:18 AM UTC

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

    Translate
    2Charlie
    2CharlieAuthor
    Inspiring
    Nov 7, 2016 7:19 PM UTC

    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>

    Translate
    Legend
    Oct 25, 2016 4:32 PM UTC

    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.

    Translate
    BKBK
    Community Expert
    Community Expert
    Oct 24, 2016 6:12 PM UTC

    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.

    Translate
    2Charlie
    2CharlieAuthor
    Inspiring
    Oct 25, 2016 12:46 PM UTC

    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.

    Translate
    BKBK
    Community Expert
    Community Expert
    Oct 26, 2016 5:14 AM UTC

    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>

    Translate