Results 1 to 2 of 2
Thread: Recursion Logic
- 02-02-2012, 02:12 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Recursion Logic
Hi, I'm writing a script in a proprietary language that is very similar to Java and I have a question about the best logical approach. The script...
Each module contains a number of objects, and each object can contain links to other objects. My task is to print all of the lowest level objects. This script does just that, but I need to know what level they are at. (Level 1, level 2, level 3, etc). So this script might print out:Java Code:string getTargets(Object obj, string finalString) { Link lnk for lnk in obj -> "*" do { Object newObj = target(lnk) finalString = getTargets(newObj,finalString) } Module newMod = module(obj) string returnString = obj."Absolute Number" returnString = newMod."Prefix" returnString "\n" finalString return returnString } Module mod = current Object obj for obj in mod do { print getTargets(obj, "") }
AAA-1
AAA-2
BBB-1
AAA-3
AAA-4
BBB-1
CCC-1
BBB-2
CCC-2
How can I, for example, make it print out:
AAA-1
AAA-2
...BBB-1
AAA-3
AAA-4
...BBB-1
......CCC-1
...BBB-2
......CCC-2
- 02-02-2012, 03:50 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Recursion Logic
I've added a counter to keep track of how many times I enter and exit the function. It works, but please let me know if there is a better way.
Java Code:string getTargets(Object obj, string finalString, int count) { count++ Link lnk for lnk in obj -> "*" do { Object newObj = target(lnk) finalString = getTargets(newObj, finalString, count) } count-- Module newMod = module(obj) string returnString = obj."Absolute Number" "" returnString = newMod."Prefix" returnString "\n" finalString returnString = "--" count "--" returnString return returnString }
Similar Threads
-
Where's the logic?
By diamonddragon in forum New To JavaReplies: 11Last Post: 01-28-2012, 04:16 AM -
Logic Help
By Barbados in forum New To JavaReplies: 6Last Post: 01-22-2012, 11:24 PM -
Need help on logic
By nn12 in forum New To JavaReplies: 6Last Post: 03-10-2011, 11:06 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks