When working on ColdFire 0.0.3, I added a new UDF that would take a string and split it into an array. This was used to split the header strings up into more manageable size items. The UDF worked fine (as far as I knew), but then Adam reported he had issues using the values on his side. Check out the UDF and see if you can spot the problem. (No fair looking at the code in the released version.)

<cffunction name="sizeSplit" output="false" returnType="array" hint="Splits a string into an array of N chars"> <cfargument name="string" type="string" required="true"> <cfargument name="size" type="numeric" required="true"> <cfset var result = arrayNew(1)>

<cfif len(arguments.string) lt arguments.size> <cfset result[1] = arguments.string> <cfreturn result> </cfif>

<cfloop condition="len(arguments.string) gt arguments.size"> <cfset arrayAppend(result, left(arguments.string, arguments.size))> <cfset arguments.string = right(arguments.string, len(arguments.string)-arguments.size)> </cfloop>

<cfreturn result> </cffunction>

The code is rather simple. You pass a string and a size for the sections. The code is smart enough to detect a small string and not bother looping. If it does have to loop, it adds the portion to the array and then cuts down the original string.

Ok, see it yet?

Well what happens if the size of my string isn't evenly divisible by the size I want? That's right - I have left overs. Adam was having issues because I was returning incomplete arrays back to him. Luckily it was easy enough to solve:

<cffunction name="sizeSplit" output="false" returnType="array" hint="Splits a string into an array of N chars"> <cfargument name="string" type="string" required="true"> <cfargument name="size" type="numeric" required="true"> <cfset var result = arrayNew(1)>

<cfif len(arguments.string) lt arguments.size> <cfset result[1] = arguments.string> <cfreturn result> </cfif>

<cfloop condition="len(arguments.string) gt arguments.size"> <cfset arrayAppend(result, left(arguments.string, arguments.size))> <cfset arguments.string = right(arguments.string, len(arguments.string)-arguments.size)> </cfloop> <cfif len(arguments.string)> <cfset arrayAppend(result,arguments.string)> </cfif>

<cfreturn result> </cffunction>

Notice now I check and see if I have anything left over. If so - I simply add it to the end of the array.