The exec
keyword or $
operator is very useful. It equates its
operand. So, for example, a string containing a valid equate expression
could be given as an argument to this operator which would then execute
the string as an equate returning any value resulting. For example, here
is a definition of strlen
(called gstrlen
) for any database field.
%%EQUATE gstrlen inputs(str) outputs(strlen(exec(str)))
The function might be called with gstrlen(%%NAME)
. Note that the
%%
operator is used to put the field name on the stack. Then within
the function the $
operator is used to evaluate the argument as
an equate expression, thus getting the contents of the field.
There is no dynamic array support in equate expressions currently, but a
simplified mechanism can be written using the exec
operator to create
named variables with a numeric index to simulate this. The size of an array
would be limited to a few hundred elements (or less, depending on how much
local variable space is available on the stack). The example below
defines
aget
to get the contents of an array element and aput
to assign a value
to an array element and example
to show the equates being used. The
definitions below use global variables and work only with string values.
%%EQUATE aget(name,index) "" >> index_str index >> index_str exec("_"+name+index_str) %%EQUATE aset(name,index,value) "" >> index_str index >> index_str exec(value+">>_"+name+index_str) %%EQUATE example /* create 5 element array, each element is set to "hello" */ 0 >> i while i < 5 do aset("ex",i,"hello") ++i >> i wend /* read back contents of array, send to stdout */ 0 >> i while i < 5 do aget("ex",i) write ++i >> i wend
Go to the first, previous, next, last section, table of contents.