slang-users mailing list

[2004 Date Index] [2004 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Accessing Functions using an array



On Dec 22, 2004, at 3:27 PM, Ben Duncan wrote:

Is it possible and how would you do it, to set up an
array that contains function names (slang, not C)
like such (pseudo code below):

define functioname_1 { .....}
define functioname_2 { .....}
define functioname_3 { .....}
......

MyArray [10] ;
MyArray[0] = "functioname_1" ;
MyArray[1] = "functioname_2" ;
MyArray[2] = "functioname_3" ;

.....
......

if(SELECTED != 0 )
  do MyArray[SELECTED]
..........


What I am trying to do is set an index to functions that the SLAG RAD/IDE generates. Each FUNCTION is really a "screen" to display. If the application contains more than one screen you would increment "SELECTED" and then the
next screen would display.

Ben,

If I understand you correctly then you want to use a reference to a function using the "&" operator and then call the routine by de-referencing the value using "@" (see also http://www.s-lang.org/doc/html/slang-3.html#ss3.3). Something like

define bob () { message ("Hi, this is bob"); }
define fred () { message ("I am fred"); }

variable funcs = Ref_Type [2];
funcs [0] = &bob;
funcs [1] = &fred;

variable i = 1;

@ funcs [i]();
i--;
@ funcs [i]();



Will call the routines fred() and then bob(). If the routines took arguments then you can say '@ funcs [i] ( arg1, ..., argn );'.

For situations like this I often use an 'Assoc_Type [Ref_Type]' array to store the function references, but it depends on how you want to access them.

Hope this helps,
Doug


_______________________________________________
To unsubscribe, visit http://jedsoft.org/slang/mailinglists.html


[2004 date index] [2004 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]