When putting together some code for the post I made a few minutes ago I
ran into trouble manipulating arrays with a cffunction.  It appears that
arrays are being passed by value.  Specifically, I originally wrote the
example with this code:


<cfset sortIdArray(rootArray)>

<cffunction name="sortIdArray" returnType="void">
        <cfargument name="arrayToSort" type="Array" required="true">
        
        <cfset var i=0>
        <cfset var j=0>
        <cfset var a = arguments.arrayToSort>
        <cfset var al = arrayLen(a)>
        <cfset var t="">
        
        <cfloop index="i" from="1" to="#al-1#">
                <cfloop index="j" from="#i+1#" to="#al#">
                        <cfif recStruct[a[i]] GT recStruct[a[j]]>
                                <cfset t = a[i]>
                                <cfset a[i] = a[j]>
                                <cfset a[j] = t>
                        </cfif>
                </cfloop>
        </cfloop>
        
</cffunction>


But it didn't work.  The sortIdArray ran, it seemed to do the proper
sorting, but later uses of the array were not sorted.  So, I changed it
to this:

<cfset rootArray = sortIdArray(rootArray)>

<cffunction name="sortIdArray" returnType="Array">
        <cfargument name="arrayToSort" type="Array" required="true">
        
        <cfset var i=0>
        <cfset var j=0>
        <cfset var a = arguments.arrayToSort>
        <cfset var al = arrayLen(a)>
        <cfset var t="">
        
        <cfloop index="i" from="1" to="#al-1#">
                <cfloop index="j" from="#i+1#" to="#al#">
                        <cfif recStruct[a[i]] GT recStruct[a[j]]>
                                <cfset t = a[i]>
                                <cfset a[i] = a[j]>
                                <cfset a[j] = t>
                        </cfif>
                </cfloop>
        </cfloop>
        
        <cfreturn a>
        
</cffunction>

and it worked fine.  Changes were in first cfset, returnType, and added
cfreturn.

This seems to indicate Arrays are passed by value.  I didn't check if
it's a deep copy (duplicate) or if it's just a first level copy.  Is
this expected/correct behavior?

Sam



----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

Reply via email to