Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating a "variable variable"

Tags:

rebol

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".

like image 527
Darrell Brogdon Avatar asked Aug 23 '13 03:08

Darrell Brogdon


People also ask

What is variable evaluation?

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.

What is variable and statement?

variable. A name that refers to a value. statement. A section of code that represents a command or action.

What is a variable in Python?

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.

Can an expression be assigned to a variable?

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.


2 Answers

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
like image 156
Brett Avatar answered Oct 01 '22 01:10

Brett


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
like image 25
kealist Avatar answered Oct 01 '22 03:10

kealist