Page 1 of 1

array keys as variable names

Posted: Thu Jul 25, 2013 5:43 am
by SteveTX
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.

Re: array keys as variable names

Posted: Sun Jul 28, 2013 6:29 am
by makeshyft
try the "do" command and construct the variable declaration using &.

Re: array keys as variable names

Posted: Sun Jul 28, 2013 10:33 am
by MaxV
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".

Re: array keys as variable names

Posted: Sun Jul 28, 2013 3:16 pm
by dunbarx
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.