Question

I'm creating a new array using cfloop and for some reason ColdFusion injects the word "YES" into a page HTML for every value in MyNewArray. So with the code below I get this "YES YES YES YES YES". How can I stop that?

<cfset MyNewArray = ArrayNew(1)>
<cfset MyNewArray2 = ArrayNew(1)>

<cfset MyNewArray[1] = "Sunday">
<cfset MyNewArray[2] = "Monday">
<cfset MyNewArray[3] = "Tuesday">
<cfset MyNewArray[4] = "Wednesday">
<cfset MyNewArray[5] = "Thursday">
<cfset MyNewArray[6] = "Friday">
<cfset MyNewArray[7] = "Saturday">

<cfloop from="2" to="6" index="i">
    <cfoutput>#ArrayAppend(MyNewArray2, MyNewArray[i])#</cfoutput>
</cfloop>
Was it helpful?

Solution

As per the docs, ArrayAppend returns a boolean - i.e. true/yes - which you are outputting by wrapping the expression in #hashes#.

You do not need to output (or indeed do anything with the result), you can just do this:

<cfloop from="2" to="6" index="i">
    <cfset ArrayAppend(MyNewArray2, MyNewArray[i]) />
</cfloop>

As a side note, a simpler way to the same thing as your code does is use duplicate to copy the array, then ArrayDeleteAt to remove the first value.

<cfset MyNewArray2 = duplicate(MyNewArray) />
<cfset ArrayDeleteAt(MyNewArray2,1) />
<cfset ArrayDeleteAt(MyNewArray2,6) />

Or, as Leigh points out in the comments, even simpler still is converting those three lines into a single ArraySlice call:

<cfset MyNewArray2 = ArraySlice(MyNewArray,2,5) />

Also consider whether DayOfWeekAsString is a useful function for whatever you're doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top