我正在使用以下内容创建一个动态变量(PHP 术语中的“变量变量”):
foo: "test1"
set to-word (rejoin [foo "_result_data"]) array 5
但是如何动态获取名为“test1_result_data”的结果变量的值?我尝试了以下方法:
probe to-word (rejoin [foo "_result_data"])
但它只是返回“test1_result_data”。
由于您的示例代码是 REBOL 2,因此您可以使用 GET 来获取单词的值:
>> get to-word (rejoin [foo "_result_data"])
== [none none none none none]
REBOL 3 处理上下文的方式与 REBOL 2 不同。因此,在创建新单词时,您需要显式处理它的上下文,否则它将没有上下文,并且在尝试设置它时会收到错误消息。这与 REBOL 2 相反,REBOL 2 默认设置单词的上下文。
因此,您可以考虑使用如下所示的 REBOL 3 代码来设置/获取动态变量:
; 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
将其作为脚本运行会产生:
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
]
上面使用 IN 的替代方法是使用 BIND:
bound-var: bind :var obj
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)