array syntax in LCB
-
- VIP Livecode Opensource Backer
- Posts: 93
- Joined: Tue Nov 17, 2009 6:47 pm
- Location: Wellington, New Zealand
array syntax in LCB
Sorry for basic questions but struggling a bit to understand array syntax.
Q 1. Am I right in understanding that array keys need to be strings - i.e. tArray["key1"] is ok ... but tArray[1] is not?
Q 2. Are nested arrays possible? This line works fine: put 7 into tArray["key1"]
but this line throws an error: put 7 into tArray["key1"]["SecondKey"]
Q 3. More generally, is the use of arrays in LCB fully documented somewhere? I've tried usual sources (API, guide, forums) but still puzzling a bit over syntax. A few code examples would be most welcome as and when available.
Q 1. Am I right in understanding that array keys need to be strings - i.e. tArray["key1"] is ok ... but tArray[1] is not?
Q 2. Are nested arrays possible? This line works fine: put 7 into tArray["key1"]
but this line throws an error: put 7 into tArray["key1"]["SecondKey"]
Q 3. More generally, is the use of arrays in LCB fully documented somewhere? I've tried usual sources (API, guide, forums) but still puzzling a bit over syntax. A few code examples would be most welcome as and when available.
Re: array syntax in LCB
In LCB, array keys must be strings.
Arrays can be nested. For example:
Can you clarify what you mean by "fully documented" in this case? I'm pretty sure that every piece of array-related syntax in LCB has code examples in its dictionary entry.
Arrays can be nested. For example:
Code: Select all
variable tArray
put {"outer": {"inner": value}} into tArray
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com
-
- VIP Livecode Opensource Backer
- Posts: 93
- Joined: Tue Nov 17, 2009 6:47 pm
- Location: Wellington, New Zealand
Re: array syntax in LCB
Fair question and the dictionary provides good descriptions and code examples of the operators, statements etc within the com.livecode.array LCB library. However it doesn't specifically talk about nested arrays, and all of the syntax examples are similar to how I am used to using arrays within LCS - e.g.: put 1 into tArray["key1"]
The example you provide with curly braces is very helpful thanks and has pointed me in the right direction. But unless I am missing something obvious, there is nothing about this somewhat different form of array syntax in the dictionary.
I stand to be corrected of course, in which case I shall swallow hard and acknowledge with the usual blushing face
The example you provide with curly braces is very helpful thanks and has pointed me in the right direction. But unless I am missing something obvious, there is nothing about this somewhat different form of array syntax in the dictionary.
I stand to be corrected of course, in which case I shall swallow hard and acknowledge with the usual blushing face
Re: array syntax in LCB
Have you read the LCB "Language Reference" guide? I think it covers it... otherwise maybe there's something missing.
We don't really have a good place to put things like descriptions of literal arrays at the moment, unfortunately. It's something that needs to be dealt with at some point but I haven't yet had the opportunity.
We don't really have a good place to put things like descriptions of literal arrays at the moment, unfortunately. It's something that needs to be dealt with at some point but I haven't yet had the opportunity.
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com
-
- VIP Livecode Opensource Backer
- Posts: 93
- Joined: Tue Nov 17, 2009 6:47 pm
- Location: Wellington, New Zealand
Re: array syntax in LCB
Yes I did read what was in the LCB Language Reference, in a section called "Array Expressions." It shows the syntax in this way:
ArrayExpression
: '{' [ <Contents: ArrayDatumList> ] '}'
ArrayDatumList
: <Head: ArrayDatum> [ ',' <Tail: ArrayDatumList> ]
ArrayDatum
: <Key: Expression> ':' <Value: Expression>
But to be honest, as an (at best) intermediate level Livecode scripter with no formal programming knowledge, this syntax looks like Greek to me. So I reverted to what I knew from LCS using square brackets and tried to build out from that. Even your simple example above using "real" code rather than the scary "generic" syntax expression makes a big difference thanks.
Having said that, I mean no criticism. It's just that I am trying to get my head around using arrays in LCB and hence the questions. I hugely appreciate the work-rate of everyone in the Livecode team, and realise that a maturing of the documentation base will take time.
ArrayExpression
: '{' [ <Contents: ArrayDatumList> ] '}'
ArrayDatumList
: <Head: ArrayDatum> [ ',' <Tail: ArrayDatumList> ]
ArrayDatum
: <Key: Expression> ':' <Value: Expression>
But to be honest, as an (at best) intermediate level Livecode scripter with no formal programming knowledge, this syntax looks like Greek to me. So I reverted to what I knew from LCS using square brackets and tried to build out from that. Even your simple example above using "real" code rather than the scary "generic" syntax expression makes a big difference thanks.
Having said that, I mean no criticism. It's just that I am trying to get my head around using arrays in LCB and hence the questions. I hugely appreciate the work-rate of everyone in the Livecode team, and realise that a maturing of the documentation base will take time.
-
- Posts: 627
- Joined: Wed Apr 24, 2013 4:53 pm
- Contact:
Re: array syntax in LCB
Thanks for asking and for the clarification, I too fumbled over this the last time I tried. It was quite a while ago and I just chalked it up to ongoing development of the LCB language at that time.
-
- VIP Livecode Opensource Backer
- Posts: 93
- Joined: Tue Nov 17, 2009 6:47 pm
- Location: Wellington, New Zealand
Re: array syntax in LCB
Thanks for the encouragement. As it turns out I'm not quite there yet but I got busy with other things. Your message has reminded me to enquire further. I'm not sure if I'm still failing to properly grasp the syntax, or if I've run into some sort of bug.
ONE
Taking code snippet:
1. variable tBoundingBoxRect as Rect
2. variable tBoundingBoxDimensionsString as String
3. variable tBoundingBoxesArray as Array
4. put the empty array into tBoundingBoxesArray
5. put the bounding box of MyWidgetPath into tBoundingBoxRect
6. put {"hold1": {"left": the left of tBoundingBoxRect}} into tBoundingBoxesArray
7. put {"hold1": {"top": the top of tBoundingBoxRect}} into tBoundingBoxesArray
8. put {"hold1": {"right": the right of tBoundingBoxRect}} into tBoundingBoxesArray
9. put {"hold1": {"bottom": the bottom of tBoundingBoxRect}} into tBoundingBoxesArray
10.
11. format tBoundingBoxesArray["hold1"]["left"] as string
12. put the result into tBoundingBoxDimensionsString
13. format tBoundingBoxesArray["hold1"]["top"] as string
14. put "," & the result after tBoundingBoxDimensionsString
15. format tBoundingBoxesArray["hold1"]["right"] as string
16. put "," & the result after tBoundingBoxDimensionsString
17. format tBoundingBoxesArray["hold1"]["bottom"] as string
18. put "," & the result after tBoundingBoxDimensionsString
Generates an error on line 11: "array key does not exist" at Runtime in an LCB script.
TWO
When I add a little more code to interrogate the keys of tBoundingBoxesArray["hold1"]:
19. variable tBoundingBoxDimensionsString as String
20. put the keys of tBoundingBoxesArray["hold1"] into tKeys
21. combine tKeys with "," into tKeysString
22. put tKeysString into tBoundingBoxDimensionsString
it returns only "bottom" which was created in line 17.
In other words tBoundingBoxesArray["hold1"]["bottom"] exists but the other three elements tBoundingBoxesArray["hold1"]["left"], tBoundingBoxesArray["hold1"]["top"], and tBoundingBoxesArray["hold1"]["right"] don't seem to exist. It appears that whenever I add a new element to tBoundingBoxesArray["hold1"] it deletes the previous element just added.
THREE
If I delete lines 11 to 16 in snippet above the code runs perfectly and (as I verify in code not shown) correctly reports the value of tBoundingBoxesArray["hold1"]["bottom"].
Am I completing misunderstanding the syntax of nested arrays in LCB here or might this be some sort of bug?
ONE
Taking code snippet:
1. variable tBoundingBoxRect as Rect
2. variable tBoundingBoxDimensionsString as String
3. variable tBoundingBoxesArray as Array
4. put the empty array into tBoundingBoxesArray
5. put the bounding box of MyWidgetPath into tBoundingBoxRect
6. put {"hold1": {"left": the left of tBoundingBoxRect}} into tBoundingBoxesArray
7. put {"hold1": {"top": the top of tBoundingBoxRect}} into tBoundingBoxesArray
8. put {"hold1": {"right": the right of tBoundingBoxRect}} into tBoundingBoxesArray
9. put {"hold1": {"bottom": the bottom of tBoundingBoxRect}} into tBoundingBoxesArray
10.
11. format tBoundingBoxesArray["hold1"]["left"] as string
12. put the result into tBoundingBoxDimensionsString
13. format tBoundingBoxesArray["hold1"]["top"] as string
14. put "," & the result after tBoundingBoxDimensionsString
15. format tBoundingBoxesArray["hold1"]["right"] as string
16. put "," & the result after tBoundingBoxDimensionsString
17. format tBoundingBoxesArray["hold1"]["bottom"] as string
18. put "," & the result after tBoundingBoxDimensionsString
Generates an error on line 11: "array key does not exist" at Runtime in an LCB script.
TWO
When I add a little more code to interrogate the keys of tBoundingBoxesArray["hold1"]:
19. variable tBoundingBoxDimensionsString as String
20. put the keys of tBoundingBoxesArray["hold1"] into tKeys
21. combine tKeys with "," into tKeysString
22. put tKeysString into tBoundingBoxDimensionsString
it returns only "bottom" which was created in line 17.
In other words tBoundingBoxesArray["hold1"]["bottom"] exists but the other three elements tBoundingBoxesArray["hold1"]["left"], tBoundingBoxesArray["hold1"]["top"], and tBoundingBoxesArray["hold1"]["right"] don't seem to exist. It appears that whenever I add a new element to tBoundingBoxesArray["hold1"] it deletes the previous element just added.
THREE
If I delete lines 11 to 16 in snippet above the code runs perfectly and (as I verify in code not shown) correctly reports the value of tBoundingBoxesArray["hold1"]["bottom"].
Am I completing misunderstanding the syntax of nested arrays in LCB here or might this be some sort of bug?
Re: array syntax in LCB
I think you're a bit confused about what the "put" command does, and what an array literal is.
Firstly, the "put" command always completely overwrites its right hand side:
If you want to overwrite only part of an array or list, you need to put only that part of the array or list on the right hand side of the "put" command, by using a subscript:
The next thing is array literals. An array literal "{ ... }" is a full array, not part of an array. If you put an array literal into an array variable, it will overwrite the whole of the current value of the variable.
As before, you use subscripts if you want to use the "put" command to overwrite only part of an array.
Going back to your example, where you are trying to construct an array, you have two options. You can use a series of "put" commands to construct the array, or you can use one big array literal -- or a combination of the two!
This snippet creates an array using an array literal, and inserts it as a new value into the "tBoundingBoxesArray".
I hope that makes sense, and please reply if you have any further questions.
Firstly, the "put" command always completely overwrites its right hand side:
Code: Select all
variable tList as List
put ["foo"] into tList
-- Now tList is ["foo"]
put ["foo", "bar"] into tList
-- Now tList is ["foo", "bar"]
Code: Select all
variable tList as List
put ["foo", "bar"] into tList
-- Now tList is ["foo", "bar"]
put "baz" into tList[2]
-- Now tList is ["foo", "baz"]
Code: Select all
variable tArray as Array
put {"key": "value"} into tArray
-- Now tArray is {"key": "value"}
put {"key2": "value2"} into tArray
-- Now tArray is {"key2": "value2"}
Code: Select all
variable tArray as Array
put {"key": "value"} into tArray
-- Now tArray is {"key": "value"}
put "value2" into tArray["key2"]
-- Now tArray is {"key": "value", "key2": "value2}
Code: Select all
variable tBoundingBoxRect as Rect
put the bounding box of MyWidgetPath into tBoundingBoxRect
variable tBoundingBoxesArray as Array
put the empty array into tBoundingBoxesArray
put {"left": the left of tBoundingBoxRect, \
"top": the top of tBoundingBoxRect, \
"right": the right of tBoundingBoxRect, \
"bottom": the bottom of tBoundingBoxRect} into tBoundingBoxesArray["hold1"]
I hope that makes sense, and please reply if you have any further questions.
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com
-
- VIP Livecode Opensource Backer
- Posts: 93
- Joined: Tue Nov 17, 2009 6:47 pm
- Location: Wellington, New Zealand
Re: array syntax in LCB
Thank you Peter. I rejigged my code based on your explanation and of course it runs perfectly
I'm ok with arrays in LCS but have found the transition to using nested arrays in LCB challenging.
For example in LCS I can simply say: put 24 into MyNewArray["set1"]["left"] and it works (even when the array is empty)
In LCB I can do that if MyNewArray["set1"]["left"] already exists (e.g. to change its value) ... but to create it in the first instance I have to use different syntax as per your explanations.
Not complaining, but to the uninitiated like me it is a bit mind-bending at first. I think it will get easier!
I'm ok with arrays in LCS but have found the transition to using nested arrays in LCB challenging.
For example in LCS I can simply say: put 24 into MyNewArray["set1"]["left"] and it works (even when the array is empty)
In LCB I can do that if MyNewArray["set1"]["left"] already exists (e.g. to change its value) ... but to create it in the first instance I have to use different syntax as per your explanations.
Not complaining, but to the uninitiated like me it is a bit mind-bending at first. I think it will get easier!
Re: array syntax in LCB
Great, I'm glad you got it working.
The main difference between LCB and LCS is that LCB is much stricter. For example, it's an error to try and access things that don't exist, such as a key in an array which has not previously been assigned. This strictness is very intentional; it's designed to help catch mistakes (for example, a typo in a array key).
The main difference between LCB and LCS is that LCB is much stricter. For example, it's an error to try and access things that don't exist, such as a key in an array which has not previously been assigned. This strictness is very intentional; it's designed to help catch mistakes (for example, a typo in a array key).
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com