Page 1 of 1
String as Variable
Posted: Thu Nov 19, 2015 11:40 pm
by nobaday
Risking dumb question of the week, who do I get the second line to interpret as the variable
on mouseUp
put "hello" into station3
put 3 into x
answer station & x // I'd like a concatenation to be interpreted as the variable station3 to answer as "hello" but it's station3 the string
answer station3 // this works, but is just here for example
end mouseUp
thanks!
Re: String as Variable
Posted: Thu Nov 19, 2015 11:44 pm
by FourthWorld
Arrays are an excellent fit for problems involving variables with names that can't be known in advance, e.g.:
Code: Select all
on mouseUp
put "hello" into station[3]
put 3 into x
answer station[x]
end mouseUp
Re: String as Variable
Posted: Thu Nov 19, 2015 11:54 pm
by dunbarx
Hi.
What Richard said.
He said what he said because an array separates the elements of the variable you are trying to reconstruct, that is, the "station" from the "3".
The "do command is much more old-fashioned. It works by forcing another level of evaluation, which can drill into what would normally be an impenetrable concatenation:
Code: Select all
on mouseUp
put "hello" into station3
put 3 into x
do "answer" && "station" & x
end mouseUp
"Do" deconstructs the contents of the variable out of its combined form. You can still, as Richard pointed out, manage the "3", or any other instance of the "concatenated" variable using this method as well. It is less modern, though.
Craig Newman
Re: String as Variable
Posted: Fri Nov 20, 2015 11:05 pm
by nobaday
Thanks, they both work!