I'm creating a dynamic variable ("Variable variable" in PHP parlance) with the following:
foo: "test1"
set to-word (rejoin [foo "_result_data"]) array 5
But how do I get the value of the resulting variable named "test1_result_data" dynamically? I tried the following:
probe to-word (rejoin [foo "_result_data"])
but it simply returns "test1_result_data".
A variable is a letter, for example x, y or z, that represents an unspecified number. 6+x=12. To evaluate an algebraic expression, you have to substitute a number for each variable and perform the arithmetic operations. In the example above, the variable x is equal to 6 since 6 + 6 = 12.
variable. A name that refers to a value. statement. A section of code that represents a command or action.
A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.
The evaluation of an expression produces a value, which is why expressions can appear on the right hand side of assignment statements. A value all by itself is a simple expression, and so is a variable.
As your example code is REBOL 2, you can use GET to obtain the value of the word:
>> get to-word (rejoin [foo "_result_data"])
== [none none none none none]
REBOL 3 handles contexts differently from REBOL 2. So when creating a new word you will need to handle it's context explicitly otherwise it will not have a context and you'll get an error when you try to set it. This is in contrast to REBOL 2 which set the word's context by default.
So you could consider using REBOL 3 code like the following to SET/GET your dynamic variables:
; An object, providing the context for the new variables.
obj: object []
; Name the new variable.
foo: "test1"
var: to-word (rejoin [foo "_result_data"])
; Add a new word to the object, with the same name as the variable.
append obj :var
; Get the word from the object (it is bound to it's context)
bound-var: in obj :var
; You can now set it
set :bound-var now
; And get it.
print ["Value of " :var " is " mold get :bound-var]
; And get a list of your dynamic variables.
print ["My variables:" mold words-of obj]
; Show the object.
?? obj
Running this as a script yields:
Value of test1_result_data is 23-Aug-2013/16:34:43+10:00
My variables: [test1_result_data]
obj: make object! [
test1_result_data: 23-Aug-2013/16:34:43+10:00
]
An alternative to using IN above could have been to use BIND:
bound-var: bind :var obj
In Rebol 3 binding is different than Rebol 2 and there are some different options:
The clumsiest option is using load
:
foo: "test1"
set load (rejoin [foo "_result_data"]) array 5
do (rejoin [foo "_result_data"])
There is a function that load uses--intern
--which can be used to bind and retrieve the word to and from a consistent context:
foo: "test1"
set intern to word! (rejoin [foo "_result_data"]) array 5
get intern to word! (rejoin [foo "_result_data"])
Otherwise to word!
creates an unbound word that is not easy to utilize.
The third option is to use bind/new
to bind the word to a context
foo: "test1"
m: bind/new to word! (rejoin [foo "_result_data"]) system/contexts/user
set m array 5
get m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With