|
|
|
An Indirect Variable reference is an advanced topic where the name of the variable you want to access is actually stored in a different variable.
For example:
Code: |
a = 123
b = a
#SHOW @{@b} |
In the above example, @b has the value of "a". So the @{@b} syntax expands to @{a} which then expands to the value of the @a variable. So the above example displays "123" on the screen.
Everything within the {} is expanded as a normal variable reference. The result of this expansion becomes the variable name that you want to return the value of. This syntax can also be nested. So you can use the indirect @{name} syntax within the {} of another indirect reference. This can get quite complicated, but is very powerful.
Normal variable references are compiled by CMUD for improved speed. With an indirect variable reference, the name of the variable is not known until runtime, so CMUD cannot compile the reference. This means that using the indirect @{a} syntax is a bit slower than the direct @a syntax. But this speed difference will not be noticeable in most situations.
Indirect references can be used to simulate arrays or other data structures. For example:
Code: |
a1 = "first element"
a2 = "second element"
a3 = "third element"
i = 2
#SHOW @{a@i} |
The a@i text within the {} is expanded and becomes a2. So a2 is used as the name of the variable, and "second element" is displayed by the above example. |
|