3

I am trying to create an array of structures, in my Application.cfm file, which can then be appended to in further pages. I am following the EasyCFM tutorial #173 by Charlie. I am using it this way:

<cfset session.box_status = arrayNew(1) />
<cfset session.box_status[1] = structNew() />
<cfset session.box_status[1].partner_id = '0' />
<cfset session.box_status[1].partner_username = '' />
<cfset session.box_status[1].status = '0' />

In my page, I am appending to the structure like so:

<cfloop from="1" to="#arrayLen(session.box_status)#" index="i">
  <cfset session.box_status[i].partner_id = ArrayAppend(i,FORM.partner_id) />
  <cfset session.box_status[i].partner_username = ArrayAppend(i,FORM.partner_username) />
  <cfset session.box_status[i].status = ArrayAppend(i,FORM.box_status) />
</cfloop>

But am getting an error:

    The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
Object of type class java.lang.Double cannot be used as an array 
2
  • 5
    check doc for ArrayAppend(), you're using it wrong. Commented Oct 19, 2014 at 11:46
  • Didn't you already ask this same question and get an answer? Their answers seems exactly the same as what I just said. Commented Oct 19, 2014 at 16:59

3 Answers 3

3

In addition to Scott's comments, you need to clarify what you are actually trying to achieve. The question asks about appending a new item, yet it looks as if parts of your code attempt to overwrite the existing structure values in position session.box_status[1].

If you really want to append a new structure to the array, there is no reason to loop. Simply create an empty structure:

<cfset newItem = structNew() /> 

... populate it with some values:

<cfset newItem.partner_id = FORM.partner_id>
... etcetera

Then append the new structure to the array. Notice, the code below does not care about the result of ArrayAppend. That is because the function modifies the array in place, and only returns true/false depending on whether the action was successful.

<cfset ArrayAppend(session.box_status, newItem)>

Update:

That said, the tutorial you are using was obviously written for an older version of CF. As @cfqueryparam pointed out, later versions support a shorthand for creating arrays and structures. Instead of using structNew(), you could simply do this:

 <cfset newItem = { partner_id = FORM.partner_id, ... etectera }>
Sign up to request clarification or add additional context in comments.

2 Comments

Just a note: Depending on the version of cf, you can even use implicit (shorthand) struct/array creation (where '[]' creates an empty array and {} creates an empty structure) like <cfset newItem = {partner_id = form.partner_id, partner_username = form.partner_username, status = form.box_status}> in cf8 and above, & <cfset newItem = {partner_id: form.partner_id, partner_username: form.partner_username, status: form.box_status}> in cf10 and above. Creating larger structs or arrays was a monotonous task before this. This is directed at the Asker, rather than @Leigh, who no doubt knows this.
@cfqueryparam - Good catch. From their original syntax, I just assumed they were using an older version that did not support "{}" and "[]". However, I see now their version is CF11. I will update my answer to use the "nicer" syntax ;-)
2

The first argument in arrayAppend() needs to be the array to which you are appending something, in your example, you are using i - which is the counter of your loop - which is a number, not an array.

2 Comments

so what could be the way i used, i tried using session.box_status[i].status instead of i, but no avail.
Because you are still not passing in an array. session.box_status[i].status is not an array.
1

Note that a common error is to pass the array name, but forget to put the pound symbols. For me, when I make the error of saying

<cfloop array="myAry" index="aryElement">

instead of the proper expression

<cfloop array="#myAry#" index="aryElement">

then the debug message java.lang.string cannot be used as an array is issued.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.