Try using the "var" keyword in your first four <cfset> statements.

<cfset var root_node = Arguments.node>
<cfset var levels = ...


-----Original Message-----
From: Cedric Villat [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 18, 2003 12:59 PM
To: CF-Talk
Subject: Recursion and Scope problem?


Ok, I'm doing a bit of recursion, and my variables seem to be overwriting
themselves, which makes no sense to me. Here is my code:

<cffunction name="getDTreeWidth" returntype="numeric">
 <cfargument name="node" type="string" required="yes">
 <cfargument name="level" type="numeric" required="yes">

 <cfset root_node = Arguments.node>
 <cfset levels = Arguments.level>
 <cfset children = Request.children>
 <cfset width = 0>

 <cfif levels EQ 0>
  <!--- No more levels to show --->
  <cfset width = 1>
 <cfelseif NOT StructKeyExists(children, root_node) OR
ArrayLen(children[root_node]) LTE 0>
  <!--- No Children --->
  <cfset width = 1>
 <cfelse>
  <cfloop from="1" to="#numChildren(root_node)#" index="a">
   <cfset width = width + getDTreeWidth(children[root_node][a], levels - 1)>
  </cfloop>
 </cfif>

 <cfreturn width>
</cffunction>


In this example, the root_node is "Homer Simpson". He has 3 children Bart,
Lisa, and Maggie. When the function is first run, Arguments.node is "Homer
Simpson". It goes through the code until it gets to the recursive call
getDTreeWidth() which then calls itself for each child. After calling
getDTreeWidth() for Bart, it seems to work. When the function returns a
number and is added to the width, the next time through Homer Simpson's
loop, the root_node is now "Bart Simpson"! How could the recursive call to
Bart Simpson overwrite Homer Simpson's original arguments? Is this a bug or
am I missing something here?

Any help would be appreciated. It may not make sense, so let me know if you
need some more clarification.

Cedric



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm?link=i:4:137532
Archives: http://www.houseoffusion.com/lists.cfm?link=t:4
Subscription: http://www.houseoffusion.com/lists.cfm?link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Get the mailserver that powers this list at 
http://www.coolfusion.com

Reply via email to