Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / How to handle unlimited nested repeating conditions?

Author
Message
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Jun 2007 02:42
I'm looking for a way to handle conditions that may repeat over and over and be nested inside each other. An example would be a 3d hierarchy where you can have bone after bone or mesh after mesh related to one another in a parent child relationship.

The goal is to be able to read back this "tree" without having to go through a set number of iterations for whatever depth of nesting there could be. I don't always want to have to dig down 100s of tiers to try and catch all the possibilities. Let's say there are two cases that the condition could be. And for each of these cases, the same two cases could be under them. Here's a pseudo code example:



A human skeleton, for example, could have a hierarchy that starts with the spine > hips > thigh > leg > ankle > foot > toe base > toe middle > toe tip etc.

This example is 10 cases deep and if each case along the way could have other branches, how do I program to adjust for various depths instead of setting a maximum depth that I would look down the tree each time?

Enjoy your day.
waffle
23
Years of Service
User Offline
Joined: 9th Sep 2002
Location: Western USA
Posted: 9th Jun 2007 08:53
the simple answer is you can't.

What you generally do is called recursion ....
Have a function call itself



that should be enough to get the idea.
In C++, each time a function is called, the function
gets a new stack space with new variables ....
In DBC, all variables are STATIC.
But, as long as you know this, this will work fine.

If you are wondering how to easily jump around a file,
Try first loading the entire file into a global variable ...
I use Dim FileLine$(MaxLines) to store file data.
And then just load the file into the Dim before actually
checking out the data.

A minor problem for you will be determine how to store your data.
One methode would be to first scan the file to determine how many
Nodes there are and then
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Jun 2007 18:33 Edited at: 10th Jun 2007 17:25
Thanks Waffle, that makes sense.

[EDIT]

I'll try setting up a recursive function.

Enjoy your day.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 30th Jun 2007 22:07
Damn you waffle!
I was gonna sound clever suggesting a recursive function.
Could you please explain what you mean by
Quote: "
In C++, each time a function is called, the function
gets a new stack space with new variables ....
In DBC, all variables are STATIC.
But, as long as you know this, this will work fine.
"


@ latch
Here is the only program I've made with good recursive functions, it should help you understand how to use them. If you already understand them have a look anyway, it's cool lol.


I make music and art. Here is a sample of my art: done in Paint! It's all I have
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Jul 2007 01:49
@Obese

Thanks for the example.

I ended up taking another approach with my program as the stack kept getting out of control - there was a limit as to how deep the recursion could go and I don't think I was managing it properly. I ended up calling the function from an outside loop and just passing the loop counter as a global and under certain conditions forcing my way out of the function and updating or decrementing the main loop counter. The info from the function was stored in an array in sequential order in a manner that fit the "tree" I wanted to build.

Enjoy your day.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Jul 2007 13:12
Is this for the DBC challenge?
I was thinking of doing something like that for storing word relationships e.g.


I make music and art. Here is a sample of my art: done in Paint! It's all I have

Login to post a reply

Server time is: 2026-07-06 00:46:15
Your offset time is: 2026-07-06 00:46:15