@@ -15,13 +15,35 @@ public foreign handler MCArrayStoreElementOfCaseless(in Value as any, inout Targ |
15 | 15 |
|
16 | 16 | public foreign handler MCArrayEvalEmpty(out Value as array) as undefined binds to "<builtin>" |
17 | 17 |
|
| 18 | +public foreign handler MCArrayRepeatForEachElement(inout Iterator as optional pointer, out Iterand as any, in Container as array) as bool binds to "<builtin>" |
| 19 | +public foreign handler MCArrayRepeatForEachKey(inout Iterator as optional pointer, out Iterand as string, in Container as array) as bool binds to "<builtin>" |
| 20 | + |
18 | 21 | -- |
19 | 22 |
|
20 | 23 | /* |
21 | 24 | Summary: Returns the keys of an array. |
22 | 25 | Target: An expression which evaluates to an array. |
23 | 26 | output: A list whose elements are the keys of <Target>. |
24 | | - Note that the list is not ordered in any way. |
| 27 | + |
| 28 | +Example: |
| 29 | + variable tArray as array |
| 30 | + put the empty array into tArray |
| 31 | + put "value1" into tArray["key1"] |
| 32 | + put "value2" into tArray["key2"] |
| 33 | + put "value3" into tArray["key3"] |
| 34 | + |
| 35 | + variable tKeys as list |
| 36 | + put the keys of tArray into tKeys |
| 37 | + sort tKeys in ascending order |
| 38 | + |
| 39 | + variable tKeysString as string |
| 40 | + combine tKeys with "," into tKeysString |
| 41 | + // tKeysString is "key1,key2,key3" |
| 42 | + |
| 43 | +References:com.livecode.sort |
| 44 | + |
| 45 | +Description: |
| 46 | +>*Note:* The resulting list is not necessarily ordered in any way. Use the <com.livecode.sort> library to sort the keys. |
25 | 47 | */ |
26 | 48 |
|
27 | 49 | syntax KeysOf is prefix operator with precedence 2 |
@@ -34,7 +56,24 @@ end syntax |
34 | 56 | Summary: Returns the elements of an array. |
35 | 57 | Target: An expression which evaluates to an array. |
36 | 58 | output: A list whose elements are the elements of <Target>. |
37 | | - Note that the list is not ordered in any way. |
| 59 | + |
| 60 | +Example: |
| 61 | + variable tArray as array |
| 62 | + put the empty array into tArray |
| 63 | + put 1 into tArray["key1"] |
| 64 | + put 2 into tArray["key2"] |
| 65 | + put 3 into tArray["key3"] |
| 66 | + |
| 67 | + variable tElements as list |
| 68 | + put the elements of tArray into tElements |
| 69 | + sort tElements in ascending numeric order |
| 70 | + |
| 71 | + // tElements is [1, 2, 3] |
| 72 | + |
| 73 | +References:com.livecode.sort |
| 74 | + |
| 75 | +Description: |
| 76 | +>*Note:* The resulting list is not necessarily ordered in any way. Use the <com.livecode.sort> library to sort the elements. |
38 | 77 | */ |
39 | 78 |
|
40 | 79 | syntax ElementsOf is prefix operator with precedence 2 |
@@ -48,6 +87,16 @@ end syntax |
48 | 87 | /* |
49 | 88 | Summary: Returns the number of elements in <Target> |
50 | 89 | Target: An expression which evaluates to an array. |
| 90 | + |
| 91 | +Example: |
| 92 | + variable tArray as array |
| 93 | + put the empty array into tArray |
| 94 | + put 1 into tArray["key1"] |
| 95 | + put 2 into tArray["key2"] |
| 96 | + put 3 into tArray["key3"] |
| 97 | + |
| 98 | + variable tVar as integer |
| 99 | + put the number of elements in tArray into tVar -- tVar contains 3 |
51 | 100 | */ |
52 | 101 |
|
53 | 102 | syntax CountElementsOf is prefix operator with precedence 1 |
@@ -94,11 +143,19 @@ end syntax |
94 | 143 | /* |
95 | 144 |
|
96 | 145 | Summary: Designates the element with key <Key> in <Target>. |
97 | | -Key: An expression which evaluates to an integer, string, or list of integers. |
| 146 | +Index: An expression which evaluates to a string. |
98 | 147 | Target: An expression which evaluates to an array. |
99 | | -output: Either locates the element container with the given key for use as the target |
100 | | - container of another operation, or evaluates the element with the given key |
101 | | - as the source of another operation. |
| 148 | + |
| 149 | +Example: |
| 150 | + variable tArray as array |
| 151 | + put the empty array into tArray |
| 152 | + put "value" into tArray["key"] |
| 153 | + |
| 154 | + variable tVar as string |
| 155 | + put tArray["key"] into tVar -- tVar contains "value" |
| 156 | + |
| 157 | +Description: |
| 158 | +Either locates the element container with the given key for use as the target container of another operation, or evaluates the element with the given key as the source of another operation. |
102 | 159 | */ |
103 | 160 |
|
104 | 161 | syntax SingletonElementOf is postfix operator with precedence 1 |
@@ -110,10 +167,82 @@ end syntax |
110 | 167 |
|
111 | 168 | -- |
112 | 169 |
|
| 170 | +/* |
| 171 | + |
| 172 | +Summary: Designates the array with zero elements |
| 173 | + |
| 174 | +Example: |
| 175 | + variable tVar as array |
| 176 | + variable tCount as int |
| 177 | + put the empty array into tVar |
| 178 | + put the number of elements in tVar into tCount -- tCount is 0 |
| 179 | + |
| 180 | +Description: |
| 181 | +Use ```the empty array``` to initialise an array variable. |
| 182 | + |
| 183 | +*/ |
| 184 | + |
113 | 185 | syntax EmptyArray is expression |
114 | 186 | "the" "empty" "array" |
115 | 187 | begin |
116 | 188 | MCArrayEvalEmpty(output) |
117 | 189 | end syntax |
118 | 190 |
|
| 191 | +-- |
| 192 | + |
| 193 | +/* |
| 194 | +Summary: Repeat over the elements of an array. |
| 195 | +Iterand: An expression of the form 'tVar in tArray' |
| 196 | + |
| 197 | +Example: |
| 198 | + variable tArray as array |
| 199 | + put the empty array into tArray |
| 200 | + put 1 into tArray["key1"] |
| 201 | + put 2 into tArray["key2"] |
| 202 | + put 3 into tArray["key3"] |
| 203 | + |
| 204 | + variable tSum as number |
| 205 | + put 0 into tSum |
| 206 | + repeat for each element tElement in tArray |
| 207 | + add tElement to tSum |
| 208 | + end repeat |
| 209 | + |
| 210 | + // tSum is 6 |
| 211 | +*/ |
| 212 | + |
| 213 | +syntax RepeatForEachElement is iterator |
| 214 | + "element" <Iterand: Expression> |
| 215 | +begin |
| 216 | + MCArrayRepeatForEachElement(iterator, Iterand, container) |
| 217 | +end syntax |
| 218 | + |
| 219 | +/* |
| 220 | +Summary: Repeat over the keys of an array. |
| 221 | +Iterand: An expression of the form 'tVar in tArray' |
| 222 | + |
| 223 | +Example: |
| 224 | + variable tArray as array |
| 225 | + put the empty array into tArray |
| 226 | + put 1 into tArray["abc"] |
| 227 | + put 2 into tArray["def"] |
| 228 | + put 3 into tArray["ghi"] |
| 229 | + |
| 230 | + variable tString as string |
| 231 | + put "" into tString |
| 232 | + |
| 233 | + variable tKey as string |
| 234 | + repeat for each key tKey in tArray |
| 235 | + put tKey after tString |
| 236 | + end repeat |
| 237 | + |
| 238 | + variable tBool as bool |
| 239 | + put tString contains "abc" into tBool -- tBool is true |
| 240 | +*/ |
| 241 | + |
| 242 | +syntax RepeatForEachKey is iterator |
| 243 | + "key" <Iterand: Expression> |
| 244 | +begin |
| 245 | + MCArrayRepeatForEachKey(iterator, Iterand, container) |
| 246 | +end syntax |
| 247 | + |
119 | 248 | end module |