Reputation: 11
I'm creating a function that displays a lot of variables with the format Variable + Variable Name.
Define LibPub out(list)=
Func
Local X
for x,1,dim(list)
list[x]->name // How can I get the variable name here?
Disp name+list[x]
EndFor
Return 1
EndFunc
Upvotes: 1
Views: 384
Reputation: 368
This will be memory intensive, but you could keep a string of variable names, and separate each name by some number of characters and get a substring based on the index of the variable in the list that you want to get. For instance, say you want to access index zero, then you take a substring starting at (index of variable * length of variable name, indexofvariable *length + length+1). The string will be like this: say you had the variables foo, bas, random, impetus the string will be stored like so: "foo bas random impetus "
Upvotes: 0
Reputation: 31147
Given a list value, there is no way to find its name.
Consider this example:
a:={1,2,3,4}
b:=a ; this stores {1,2,3,4} in b
out(b)
Line 1: First the value {1,2,3,4}
is created. Then an variable with name a
is created and its value is set to {1,2,3,4}
.
Line 2: The expression a
is evaluated; the result is {1,2,3,4}
. A new variable with the name b
is created and its value is set to `{1,2,3,4}.
Line 3: The expression b
is evaluated. The variable reference looks up what value is stored in b
. The result is {1,2,3,4}
. This value is then passed to the function out
.
The function out
receives the value {1,2,3,4}
. Given the value, there is no way of knowing whether the value happened to be stored in a variable. Here the value is stored in both a
and b
.
However we can also look at out({1,1,1,1}+{0,2,3,4})
.
The system will evaluate {1,1,1,1}+{0,2,3,4}
and get {1,2,3,4}
. Then out
is called. The value out
received the result of an expression, but an equivalent value happens to be stored in a
and b
. This means that the values doesn't have a name.
In general: Variables have a name and a value. Values don't have names.
If you need to print a name, then look into strings.
Upvotes: 1