Page 1 of 1

Interior values of arrays redux

Posted: Wed May 20, 2015 8:12 pm
by dunbarx
Just so I am sure I understand:

Code: Select all

on mouseUp

-- second level of array
   put "5" into buttonArray ["a"]["b"]
   put "6" into buttonArray ["a"]["c"]
   put "7" into buttonArray ["a"]["d"]
   
--third level of array
   put "X" into buttonArray ["a"]["b"]["h"]
   put "Y" into buttonArray ["a"]["c"]["j"]
   put "Z" into buttonArray ["a"]["d"]["k"]

   answer  buttonArray ["a"]["b"] --yields empty unless third level is commented out, that is, never loaded
end mouseUp
The value for buttonArray ["a"]["b"], which is ostensibly "5", is not available, even in the debugger, if the third level of values is set into the array. If I comment out that third level, I can see the "5" in buttonArray ["a"]["b"], but that is no surprise, since no third level was ever created.

My question is this: Is it so? Do "intermediate" values of an array become non-existent if deeper values are installed? They cannot be retrieved even if the third-level elements are deleted:

Code: Select all

on mouseUp
   put "5" into buttonArray ["a"]["b"]
   put "6" into buttonArray ["a"]["c"]
   put "7" into buttonArray ["a"]["d"]
   
   put "X" into buttonArray ["a"]["b"]["h"]
   put "Y" into buttonArray ["a"]["c"]["j"]
   put "Z" into buttonArray ["a"]["d"]["k"]
   
   delete variable buttonArray ["a"]["b"]["h"]
   delete variable buttonArray ["a"]["c"]["j"]
  delete variable buttonArray ["a"]["d"]["k"]
   answer  buttonArray ["a"]["b"] --still returns empty
end mouseUp
I am not interested in the keys of interior values, such as:

Code: Select all

  the keys of buttonArray ["a"]
These are always available at any level. I am interested in that missing "5".

Craig Newman

Re: Interior values of arrays redux

Posted: Wed May 20, 2015 8:36 pm
by SparkOut
A variable can only have one content. You set the contents of buttonArray["a"]["b"] to 5, then you overwrite it with new contents, which happens to be another array, so you get another depth to traverse, but the "b" key has still only one value. But when you delete the ["h"] array (which are the entire contents of the "b" level) there is nothing to restore to find that elusive 5 again.

Re: Interior values of arrays redux

Posted: Wed May 20, 2015 9:04 pm
by FourthWorld
Try:

Code: Select all

answer  (buttonArray ["a"]["b"] is an array)
An array is a collection of key-value pairs, in which the key is a string and the value can be anything, even another array.

Arrays have no string value, and the answer command expects a string. So to give answer something it can work with, you can show "true" or "false" as I've done above, or get the keys, or any other array operation which will result in a string.

Re: Interior values of arrays redux

Posted: Wed May 20, 2015 11:06 pm
by dunbarx
@SparkOut. OK. I see that, I just do not like it. I had my own ideas and hopes about how an array was constructed, sort of a layered variable that retained its "interior" values. But I get what you are saying.

@Richard. Surely the answer command can indeed get a string value from an array directly:

Code: Select all

put "X" into buttonArray ["a"]["b"]["h"]
   answer  buttonArray ["a"]["b"]["h"]
But I am sure that what you meant is that the "interior" value of buttonArray["a"], that is, ["b"], even if once a string value, becomes an array once the overall array variable develops another level. Thus it can no longer be considered a string value, even though it once was. As per what I said above to SparkOut, I had the idea that the original value was somehow retained. But the more I think about it, how could a value be incorporated into a multi-level variable if it kept its value? It would have to devolve to an array "variable" in its own right, and therefore that old string value has to be eliminated.

Thanks to both. I will shift my array gears.

Craig

Re: Interior values of arrays redux

Posted: Wed May 20, 2015 11:20 pm
by FourthWorld
dunbarx wrote:@Richard. Surely the answer command can indeed get a string value from an array directly:

Code: Select all

put "X" into buttonArray ["a"]["b"]["h"]
   answer  buttonArray ["a"]["b"]["h"]
Yes, if those values are strings. The example I had used was the one you reported showed empty, which is understandable since the value isn't a string but an array:

Code: Select all

answer  buttonArray ["a"]["b"] --still returns empty
But I am sure that what you meant is that the "interior" value of buttonArray["a"], that is, ["b"], even if once a string value, becomes an array once the overall array variable develops another level. Thus it can no longer be considered a string value, even though it once was. As per what I said above to SparkOut, I had the idea that the original value was somehow retained. But the more I think about it, how could a value be incorporated into a multi-level variable if it kept its value? It would have to devolve to an array "variable" in its own right, and therefore that old string value has to be eliminated.
If it's easier, just think of arrays as key value pairs, in which each pair has one key and one value, even if that value is another array.

Doing this:

Code: Select all

put "x" into foo[1]["a"]
answer foo[1]["a"]
...is the same as doing:

Code: Select all

put "x" into tSomeArray["a"]
put tSomeArray into foo[1]
put foo[1] into tSomeArray
answer tSomeArray["a"]
It's just easier for us (and more efficient for the computer) to not have to copy the array value stored in foo[1].