String as Variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
nobaday
Posts: 36
Joined: Mon May 09, 2011 3:23 pm

String as Variable

Post by nobaday » Thu Nov 19, 2015 11:40 pm

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!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: String as Variable

Post by FourthWorld » Thu Nov 19, 2015 11:44 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: String as Variable

Post by dunbarx » Thu Nov 19, 2015 11:54 pm

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

nobaday
Posts: 36
Joined: Mon May 09, 2011 3:23 pm

Re: String as Variable

Post by nobaday » Fri Nov 20, 2015 11:05 pm

Thanks, they both work!

Post Reply