我创建(在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”。
我创建(在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”。
probe do (rejoin [foo "_result_data"])
从http://www.rebol.com/docs/core23/rebolcore-4.html#section-4.6
在雷博尔3结合比Rebol的2个不同的并有一些不同的选项:
的笨选择是使用load
:
foo: "test1"
set load (rejoin [foo "_result_data"]) array 5
do (rejoin [foo "_result_data"])
有一个负载使用的函数 - intern
- 可用于绑定并从一致的上下文中检索单词:
foo: "test1"
set intern to word! (rejoin [foo "_result_data"]) array 5
get intern to word! (rejoin [foo "_result_data"])
否则to word!
创建一个不容易使用的未绑定单词。
第三个选择是使用bind/new
这个词结合上下文
foo: "test1"
m: bind/new to word! (rejoin [foo "_result_data"]) system/contexts/user
set m array 5
get m
我发现自己在这些情况下恢复到LOAD很多,但是有没有使用BIND的公式?有时候你有些可能是WORD的东西!或SET-WORD!等等,并将其串化并加载它非常尴尬。 – HostileFork
根据聊天讨论,避免LOAD的一种方式是使用INTERN(它的来源提供了对单词上下文的更多元素控制)。 “套在文字上! “任意词”“一些东西”可用于词语! “任意词” – rgchris
由于您的示例代码是REBOL 2,你可以使用GET来获得这个词的价值:
>> get to-word (rejoin [foo "_result_data"])
== [none none none none none]
REBOL 3处理与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
]
替代使用上面本来可以使用BIND:
bound-var: bind :var obj
我强烈推荐这个答案。除了反对使用'为这样的事情做'之外,'set'和'get(或'unset)之间有一个很好的平衡。 – rgchris
Bah。现在我明白了。谢谢。 :) –