In a PRACTICE script, is there an easy way to iterate over a "list of variables/macros"? For example:
; this "list" changes...
PRIVATE &var1 &var2 &var3
; this implementation shall not change
; do something with &var1 &var2 &var3 ...
I would like to be able to add variables without the need to touch the code that iterates over the variables.
I assume you are thinking about a subroutine or separate script that should process a variable numbers of parameters. As the macros are expanded at the time of execution, the callee will not see the macros, but only their contents. So what the callee sees is a list of space separated values. You can write a subroutine that parses the parameters as one string and later separates them using STRing.SPLIT(). Example:
PRINT "Print 1 item:"
GOSUB PrintItems ONE
PRINT "Print 3 items:"
GOSUB PrintItems ONE TWO THREE
ENDDO
PrintItems:
PRIVATE &list &count
ENTRY %LINE &list
&count=STRing.COUNT("&list"," ")+1
&index=0
RePeat
(
PRIVATE &item
&item=STRing.SPLIT("&list"," ",&index)
PRINT "&item"
&index=&index+1
)
WHILE &index<&count
RETURN
Output:
Print 1 item:
ONE
Print 3 items:
ONE
TWO
THREE
It is possible to use other separation characters as well, by replacing the BLANK in STRing.COUNT() and STRing.SPLIT(), and of course in the subroutine call.