I want to alter a series of variables from a loop, but the following code is not working:
a: 10
b: 20
c: 30
print reduce [a b c] ; output is 10 20 30 as expected
varnames: [a b c] ; make a series of variables
foreach i varnames [ ; loop to convert each to 0
i: 0
]
print "After conversion loop: "
print reduce [a b c] ; values are still 10 20 30 (expected 0 0 0)
After running this code, I'd expect the values of a
, b
, c
to be changed, but they're not:
>> print reduce [a b c]
10 20 30
Where is the problem?
>> set varnames: [a b c] [10 20 30]
== [10 20 30]
>> foreach i varnames [set i 0]
== 0
>> reduce varnames
== [0 0 0]
You've given i a value from the loop, and then given it a different value of 0, but you actually haven't altered the word that i referred to. set
allows you to do this.