[REBOL] Re: variable variable
From: joel:neely:fedex at: 13-Jun-2001 0:34
Ammon Cooke wrote:
> Hold on while I, with my weak english skills, try to words
> this so the gurus
> know what I am talking about....
>
> Ok, here we go...
>
> What I need is this:
>
> I am getting a value from a text-list:
>
> lst1-rnd: lst1/text: random/only lst1/data
>
> now create a variable:
>
> do reduce[to-set-word lst1-rnd: 1]
>
> now how do I access the value of the value of lst1-rnd
> without explicitly calling it??
>
I'm still unclear what you mean by the phrase "without
explicitly calling it". Let's make a simple example
>> pig
** Script Error: pig has no value
** Near: pig
>> cow
** Script Error: cow has no value
** Near: cow
>> duck
** Script Error: duck has no value
** Near: duck
; just to prove there's nothing up my sleeve... ;-) Now,
>> some-word: random/only [pig cow duck]
== duck
>> do reduce [to-set-word some-word "quack"]
== "quack"
At this point, we've set the word DUCK (which formerly was
not set to anything) to the string "quack". We can prove
this by
>> duck
== "quack"
To get at this word (and its current value) we must somehow
remember what we called it. Its "name" is still in SOME-WORD,
so we can do one of the following:
>> reduce reduce [some-word]
== ["quack"]
>> print reduce [some-word]
quack
Once we change the value of SOME-WORD, we can't use it to
get back to DUCK and hence to "quack". So, if you're going
to change SOME-WORD, be sure to save its value somewhere,
such as in a block. For example:
repeat i 5 [
some-word: random/only [pig cow duck chicken horse dog cat]
do reduce [to-set-word some-word i]
if none? find barnyard some-word [append barnyard some-word]
]
== [duck chicken cat dog]
foreach indirect-word barnyard [
print [indirect-word do reduce [indirect-word]]
]
duck 1
chicken 3
cat 4
dog 5
If you want to keep something, you have to remember where you
put it, one way or another!
> PS the random function always picks the items from the list
> in the SAME order. The reason I tried the random function
> is to avoid that. What am i doing wrong??
>
Try using the /SEED refinement to RANDOM.
-jn-
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com