Results 1 to 2 of 2

Thread: Recursion Logic

  1. #1
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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...
    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, "")
    }
    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:
    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

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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

  1. Where's the logic?
    By diamonddragon in forum New To Java
    Replies: 11
    Last Post: 01-28-2012, 04:16 AM
  2. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-22-2012, 11:24 PM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •