Skip to main content
Known Participant
July 3, 2011
Question

Understanding Array Update

  • July 3, 2011
  • 2 replies
  • 906 views

Hi,

I started building an app using the Lynda.com's: Coldfusion: beyond the basics. The "basic" courses were nice to begin with but I quickly found out that they left quiet a bit aside, the "beyond the basics" works, but I found that a lot of the basics where not explained. Now I reached a dead end, I decided to go back to the basics to try to make things slightly clearer.

So far I have:

<!--- if the array does not exist, create it --->

<cfif not isdefined("ar1")>
<cfset ar1 = arraynew(1)>
</cfif>

<!--- Add a form to update the Array --->

<cfif isdefined ("form.ada")>
<cfset index= Arraylen(ar1)>

<cfset temp= arrayappend(ar1, "#form.ada#")>

</cfif>

The rest is to display the array.

Now, by now I suppose  that those who bothered to read this far already understood what I did wrong, thing is... I don't

When I enter the data the array is updated, BUT... if I try adding another data it keeps replacing the old one...

I've tried various formats including:

<cfset ar1[index+1]=#form.ada#>

To no avail...

So far I've found that "index" does not reflect the length of the Array, yet the data is displayed both by:

<cfdump var="#Ar1#">

and

<cfloop index="valval" from="1" to="#Arraylen(ar1)#">
...
</cfloop>

I'll be able to handle myself for the rest but this puzzles me.

Thank you

    This topic has been closed for replies.

    2 replies

    Inspiring
    July 3, 2011

    Fernis has given you good advice, but let's resolve the error in your logic from your own code, too.

    So far I've found that "index" does not reflect the length of the Array

    Well it DID when you first set it, but you're not keeping it up to date.

    You did this:

    <cfset ar1 = arraynew(1)>
    [...]
    <cfset index= Arraylen(ar1)>

    So... you've set it here.  As it's a new array, you will be setting index to zero.

    Then you go on to do this:

    <cfset temp= arrayappend(ar1, "#form.ada#")>

    So the length of the array is now 1, but you have a variable index which holds 0.  It's now out of date.

    So when you do this:

    <cfset ar1[index+1]=#form.ada#>

    You are setting the element at 0+1 (=1) to be the new value. However you've already got an element at position 1 in the array, so all you're doing is overwriting it.  Which is what you're seeing.

    The best solution is to do what Fernis said: if you want to append an element onto the end of an array, just use arrayAppend().  That's what it's for.  One would assign a value via a specific array index if one actually wanted to set element n to be something other than what it is already (replacing a value), or one would also - obviously - address the elements by index when accessing them (ie: for read operations).

    --

    Adam

    Fernis
    Inspiring
    July 3, 2011

    Instead of direct assignment functions (<cfset>, etc.) , use Array related functions.

    ArrayAppend() is what you probably need.

    http://www.quackit.com/coldfusion/tutorial/coldfusion_arrays.cfm should get you forward.

    --

    -Fernis

    Kryss2099Author
    Known Participant
    July 3, 2011

    Hi,

    Thanks for your answers...

    Fernis... I've already read the page you mentionned... I may have missed something, but they mention that in order to update the Array the correct "format" should be something like

    <cfset ArrayAppend(faq, "How to modify an array?")>(which I took directly from the page you mentionned).

    I had tried already the format:

    <cfset ArrayAppend(ar1, "#form.ada#")>

    and it gives me the same result, the value is replaced, not appended.

    Inspiring
    July 3, 2011

    I had tried already the format:

    <cfset ArrayAppend(ar1, "#form.ada#")>

    and it gives me the same result, the value is replaced, not appended.

    I am sorry mate, I do not believe you.

    Please post a reproduction case for this: stand-alone and self-contained code that demonstrates this happening.

    --

    Adam