Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle loops within recursions 1

Status
Not open for further replies.

csteinhilber

Programmer
Aug 2, 2002
1,291
0
0
US
I have a ColdFusion 5 user-defined function (ie - written in CFSCRIPT) that recurses itself down a tree of data.

Everything is working great. 'cept now I have a need to add a loop within the function for other processing... and I'm trying to figure out how to handle it.

Example:
Code:
<CFSCRIPT>
   function ProcessDataBranch(...){
       if (somecondition EQ true){
                :
	  for (i = 1;i LTE 5;i = i + 1){
	     myVar = myVar & ProcessDataBranch(...);
	  }
       }
                :
       return myVar;
   }
</CFSCRIPT>

Problem is, since ColdFusion doesn't seem to have a concept of private vs. public variables... won't any values of #i# in the loop of the parent recursion get overwritten by values of #i# in the children??

I haven't actually coded it yet, but before I put a lot of work into it, I thought maybe someone had a pointer for accepted ways to handle such a situation. Or maybe it's not even an issue and everything would work just fine (though I really truly doubt it).

Thanks in advance!

-Carl
 
You can use the VAR keyword to declare a variable private to a function:
Code:
<CFSCRIPT>
   function ProcessDataBranch(...){
       if (somecondition EQ true){
                :
      for (var i = 1;i LTE 5;i = i + 1){
         myVar = myVar & ProcessDataBranch(...);
      }
       }
                :
       return myVar;
   }
</CFSCRIPT>
-Tek
 
Thanks Tek!

I had no idea you could declare private vars in CFSCRIPT.

I've had a few problems... like I can't actually use:
Code:
   for (var i = 1;i LTE 5;i = i + 1){
   }
for some reason... CFCHOKE. I have to declare the var at the top of the function, then simply use it in the for loop:
Code:
   var i = 1;
       :
       :
   for (i = 1;i LTE 5;i = i + 1){
   }
can't figure out why. I built a test function pretty much identical to yours above and
Code:
for (var i = 1...
works just fine there, so I don't know what's going on.

Also, round about the third itteration of my actual, ColdFusion suddenly throws an exception saying that ProcessDataBranch is not the name of a function. Yet, again, the test function I built itterates 50 times and beyond without complaint.

So I obviously have some quirky code that I need to work on. But your advice is exactly what I needed. I can tell from my testing that it will be on a great order of magnitude faster than the custom tag solution.

Thanks much,

-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top