Normal Topic Calling dynamic sub routine. (Read 664 times)
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Calling dynamic sub routine.
Mar 5th, 2004 at 6:34am
Print Post Print Post  
I could use some creative minds on this one please.  I have hit a brick wall.....maybe too close to it and need to take a break?  ???

I need to be able to call a subroutine that is dynamic in nature.

I have over 100 pre-defined subroutines.  This number will probably increase over time.  I need to take user input from keyboard and use the input to run a subroutine with the name they have entered.   Input may possibly come from a random word generator vs. keyboard input.

Examples:
If the entry is  "abc" I want the action to be subroutine abc()
If the entry is "hello world" I want the action to be subroutine hello world()
If the entry is  "bad boy" I want the action to be subroutine bad boy()
Error trapping will exist if entry is a non-existent function.

If that was the whole spec, that would not be a problem.

But here is the twist:  For reasons that are not important, but part of the design spec, the entry person cannot be provided with a list of valid entries.  So this eliminates a popup list or UserSelect method.

It is no problem to take the entry value and convert it into a declared variable,  but that variable cannot be called as a subroutine.  I am pretty sure this is the same issue that I asked about on 2/22.  So, I probably need to find a work-around.  Here are the details:

I could make a hundred+ IF statements like:
If entry = "hello world" then hello world() else
If entry = "bad boy" then bad boy() else
If entry = "abc" then abc() else
........
........
ErrorTrapping()
End if

OR I could use @Select with a similar listing.

But besides the time for the programming effort, these solutions are hardcoded.  When new subroutines are created, then this code would also need to be modified.  I am looking to create/maintain subroutines separate from the database so changes can be done without having to modify the program code.

One off the wall thought I have now is to possibly take the entry value, use @shell to open another copy of Seseme and pass the entry as a value to autoexecute some type of macro calling the subroutine somehow.   Don't even want to think of the complexity that will involve, so I won't spend any time on that option yet.

I am sure your suggestions will be interesting.  I just hope that Mark/Erika don't come back and say "You can't do that at all!"  If they do, then maybe Tom can use this as a "Monthly Puzzler" in Inside Sesame? Grin

I think what I need is a new function like:
@SubRoutine(x) where "x" is a variable containing the name of a subroutine. 
@Function(x) would provide the same dynamic tools for functions also.
Too late to get into 1.0.2?  Roll Eyes
« Last Edit: Mar 5th, 2004 at 9:38am by Bob_Hansen »  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Justin_ICC
Junior Member
**
Offline



Posts: 95
Joined: Feb 5th, 2004
Re: Calling dynamic sub routine.
Reply #1 - Mar 5th, 2004 at 4:34pm
Print Post Print Post  
At a quick scan this is what i've been able to come up with as a means to try and accomplish your goal. (with current functionality). What you need to do is create a macro for subroutine you create (that you want to execute dynamically). You can then use the @macro function to execute that macro based a on a text string:

ie
user types in "abc" -> stored in variable ProcToRun
executes macro "abc" with this code @Macro(ProcToRun)
"abc" macro calls the subroutine you defined abc()

Does this help at all?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Calling dynamic sub routine.
Reply #2 - Mar 5th, 2004 at 9:22pm
Print Post Print Post  
Bob,

What you are describing would be dynamically generated randomized computed function pointers. These sound cool, but in the long run, these can be very very bad. They make debugging and maintaining your code a nightmare. Let alone somebody else's code! Talk to anybody who has ever had to deal with a pile of someone else's spaghetti Fortran and ask them how they feel about computed GoTo's.  Roll Eyes

In actuality, using a well-structured If..Then..Else statement is less work and more maintainable than the other solutions you describe. Adding a new subroutine simply involves adding a line to the conditional. Not really all that much work. It also allows you to call the same subroutine under more than one condition.

Subroutine BranchMe(vKey as String)
    If(vKey = "sub1") Then Sub1()
    Else If(vKey = "sub2") Then Sub2()
    Else If(vKey = "sub3") Then Sub3()   
End Subroutine

Besides, if we add this to SBasic, and Andreas finds out about it, he may actually levitate across the ocean to "discuss" it with us.  He's normally an extremely non-violent person, but a developer of his caliber can only take so much...  Grin Wink
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Calling dynamic sub routine.
Reply #3 - Mar 6th, 2004 at 5:21am
Print Post Print Post  
Thanks for the macro idea Justin_ICC.  That's the kind of thinking outside the box I am looking for.  Will try that this weekend and let you know the results.

I never thought that @SubRoutine() or @Function() would result in "randomized computed function pointers."  I thought that internal stacks were supposed to deal with that.   Learning something new every day.  But, that is an internal area where I have no expertise, so I guess I will have to accept that for now.

Thus the need for some more creative approaches for a solution..........
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged