Byron Ferguson
Byron Ferguson

Reputation: 21

Coldfusion ArraySet() v. user defined cffunction

I replaced this user-defined CF function that I found.

<cffunction name="initializeValues" returntype="array" output="false">
<!--- Initialize all elements of an array to zero --->
  <cfargument name="inArray" required="yes" type="array">
  <cfargument name="inCount" required="yes" type="numeric">

  <cfloop index="i" from="1" to="#inCount#">
    <cfset inArray[i] = 0>
  </cfloop>

  <cfreturn inArray>
</cffunction>

with the built-in CF9 function

ArraySet(arrayName, startingIndex, endingIndex, 0)

however, the final results differed somehow and threw an exception on the user page.

How do these functions differ?

Upvotes: 0

Views: 122

Answers (1)

Leigh
Leigh

Reputation: 28873

You need to provide more specifics. What do you mean by "differed somehow" and what exception was thrown?

Without knowing more, one primary difference is that ArraySet modifies the array "in place". Whereas the cffunction does not. With udf's arrays are passed "by value", so the function has no effect on the original array object. Instead you must capture the returned array. (Of course, then it does not make any sense to require an array argument in the first place, but .. that is a different topic.)

    arr = initializeValues([], 10);
    writeDump(arr);

Since ArraySet modifies the array in place, it returns true/false. Perhaps you are mistakenly capturing the result of ArraySet and overwriting your array object?

    // wrong: overwrites the array
    arr = [];
    arr = ArraySet(arr, 1, 10, 0);
    writeDump(arr); 

    // correct 
    arr = [];
    ArraySet(arr, 1, 10, 0);
    writeDump(arr); 

Upvotes: 2

Related Questions