Page 1 of 1

Interior values in arrays?

Posted: Fri May 08, 2015 5:40 pm
by dunbarx
I am back in the beginners forum again.

I use multi-level arrays all the time. Apparently I do not understand them. If I make an array:

Code: Select all

put 10 into myArray["a"]["x"]
   put 20 into myArray["a"]["y"]
   put 30 into myArray["a"]["z"]
   
   put 40 into myArray["b"]["x"]
   put 50 into myArray["b"]["y"]
   put 60 into myArray["b"]["z"]
Easy to get, say, myArray["b']["y"], which would be 50.

But how do I get a list of the "secondary keys" if I may call them such, of the array? In other words, how do I get "x", "y" and "z", which are the "subKeys" of "a"?. You cannot ask for:

answer myArray["a"] to see:

"x"
"y"
"z'"

because that code returns empty. You can see these in only one place that I know of, and that is in the debugger, where arrays are fully deconstructed, including all intermediate hierarchal structure, when stepping through.

On a related issue, since there are only two delimiters associated with the "combine" command, I cannot in one step fully display the above array in a field. I would have to extract all that data and format under script control.

No problem there, but is there something simple I am just not aware of?

Craig Newman

Re: Interior values in arrays?

Posted: Fri May 08, 2015 7:42 pm
by FourthWorld
An array is a collection of name-value pairs, in which the name is a string up to 255 chars long and the value can be any data, textual or binary, of nearly any length, including another array.

Any expression that expects a string won't show anything with an array, since an array isn't a string value.

But we can obtain part of an array as a string, such as the keys, and display that part.

So as with your myArray example, trying:

Code: Select all

put myArray
...will fail since the array isn't a string.

But you can use:

Code: Select all

put the keys of myArray
Similarly, when the value of an array element is another array, you can't display that array itself but you can obtain its keys:

Code: Select all

put the keys of myArray["a"]
Whether querying a variable which is an array, or any value within an array which is another array, the options for working with the data are consistently applied.

Re: Interior values in arrays?

Posted: Sun May 10, 2015 1:11 am
by dunbarx
Richard

Thanks. That is the piece I was missing. It is intermediate keys that report intermediate "levels" of a multi dimensional array. I actually used the term in my original query. Should have picked that up myself.

Usually this information is not needed, at least for me, since the fully nested value is the information of interest, and intermediate keys are only vectors to access them. But I just had a case where I wanted to know third level keys, and using the debugger was clunky, to say the least.

Craig

Re: Interior values in arrays?

Posted: Sun May 10, 2015 2:23 am
by FourthWorld
I've found this helpful:
http://lessons.runrev.com/m/4071/l/2102 ... dable-form

Before LiveCode I'd not worked with associative arrays in other languages, and at first I found them somewhat daunting, so used to working with data that can be seen in one place.

A visual decomposition like that sample script can be very helpful, even today when I make arrays too deeply nested to remember how all the parts are laid out. :)