我創建(在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。現在我明白了。謝謝。 :) –