array keys as variable names

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
SteveTX
Posts: 170
Joined: Sun Jan 17, 2010 9:00 pm

array keys as variable names

Post by SteveTX » Thu Jul 25, 2013 5:43 am

I have a multidimensional array that is extremely long and prone to syntax errors. I would like to take the keys at a particular dimension and make them locals for processing, and store their array value as the value for that variable. The problem is that I am creating variable names from variables themselves, which results in a 'bad destination' error. Is there a good way around this?

Code: Select all

.	.	.	[dimension] => Array
                  .	.	.	.	[lion] => courage
                  .	.	.	.	[scarecrow] => brains
                  .	.	.	.	[tinman] => heart
so we would end up with a variable called 'theLion' which hold value 'courage', and so on.

makeshyft
Posts: 222
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: array keys as variable names

Post by makeshyft » Sun Jul 28, 2013 6:29 am

try the "do" command and construct the variable declaration using &.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: array keys as variable names

Post by MaxV » Sun Jul 28, 2013 10:33 am

You can use variable for keys:

Code: Select all

put "lion" into temp
put "courage" into temp2
put temp2 into myArr[temp]
This way you created myArr["lion"]="courage".
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: array keys as variable names

Post by dunbarx » Sun Jul 28, 2013 3:16 pm

Hi.

I don't think anyone here is getting what you want, including me. Does this address your query?

Code: Select all

on mouseUp
   put "wet" into myArray[test][testWet][sponge]
    put "up" into myArray[test][testUp][down]
    put "dry" into myArray[test][testDry][sand]
     put "sideWays" into myArray[test][testUp]["down"]
If you step through the script, you can see that the value for "testUp" in the array is set, and then changes at the level "down".

Craig Newman

EDIT

Actually, I am not sure I actually did anything significant.

Post Reply