|
1 | 1 | # ARRAY |
2 | 2 |
|
3 | | -> ARRAY [var | expr] |
| 3 | +> A = ARRAY [s | expr] |
4 | 4 |
|
5 | | -Creates a ARRAY or MAP variable from the given string or expression. |
| 5 | +Creates a ARRAY or MAP variable from the given string `s` or expression `expr`. The ARRAY command supports JSON (Javascript object notation) syntax. The MAP provides value-key pair access along with array or dotted notation. |
6 | 6 |
|
7 | | -The ARRAY command supports JSON (Javascript object notation) syntax. The MAP provides value-key pair access along with array or dotted notation. |
8 | | -The MAP can be converted back into a JSON string using the STR command. You can test whether a variable is a MAP using the ISMAP command. |
| 7 | +See STR for converting a MAP variable into a JSON string. |
| 8 | + |
| 9 | +### Example 1: JSON string to MAP |
9 | 10 |
|
10 | 11 | ``` |
11 | 12 | a = array("{Name: Alice, Age: 20}") |
12 | 13 | print a |
13 | 14 | print a.Name + " is " + a.Age + " years old" |
| 15 | +
|
| 16 | +' Output: |
| 17 | +' {"Age":20,"Name":"Alice"} |
| 18 | +' Alice is 20 years old |
14 | 19 | ``` |
15 | 20 |
|
16 | | -Instead of using ARRAY, the following shorter syntax will give the same result: |
| 21 | +### Example 2: JSON with nested MAP |
17 | 22 |
|
18 | 23 | ``` |
19 | | -a = {Name: Alice, Age: 20} |
20 | | -print a |
21 | | -print a.Name + " is " + a.Age + " years old" |
| 24 | +a = Array("{x:1, y:3, down:{x:4, y:7}}") |
| 25 | +print a ' Output: {"down":{"y":7,"x":4},"x":1,"y":3} |
22 | 26 | ``` |
23 | 27 |
|
24 | | -The following example shows a possible JSON representation describing a person: |
| 28 | +### Example 3: Array string to array |
25 | 29 |
|
26 | | -~~~ |
27 | | -{ |
28 | | - "firstName": "John", |
29 | | - "lastName": "Smith", |
30 | | - "isAlive": true, |
31 | | - "age": 25, |
32 | | - "address": { |
33 | | - "streetAddress": "21 2nd Street", |
34 | | - "city": "New York", |
35 | | - "state": "NY", |
36 | | - "postalCode": "10021-3100" |
37 | | - }, |
38 | | - "phoneNumbers": [ |
39 | | - { |
40 | | - "type": "home", |
41 | | - "number": "212 555-1234" |
42 | | - }, |
43 | | - { |
44 | | - "type": "office", |
45 | | - "number": "646 555-4567" |
46 | | - }, |
47 | | - { |
48 | | - "type": "mobile", |
49 | | - "number": "123 456-7890" |
50 | | - } |
51 | | - ], |
52 | | - "children": [], |
53 | | - "spouse": null |
54 | | -} |
55 | | -~~~ |
56 | | - |
57 | | -Example 1: |
58 | | - |
59 | | -~~~ |
60 | | -Def uline(text) = Cat(2) + text + Cat(-2) ' Underline text |
61 | | -Cls |
62 | | -? uline("1-Dimension Map Array:") |
63 | | -? |
64 | | -a = Array("{x:100, y:300, title:top-left}") |
65 | | -? "a is: "; a |
66 | | -? "a is map? "; Ismap(a) |
67 | | -? |
68 | | -? "a.x is: "; a.x |
69 | | -? "a.y is: "; a.y |
70 | | -? "a.title is: "; a.title |
71 | | -? |
72 | | -a.x = "N/A" |
73 | | -? "a.x is now: "; a.x |
74 | | -? |
75 | | -?:?:? " Press any key..." |
76 | | -Pause |
77 | | -Cls |
78 | | -? uline("1-Dimension Map Array, with 1-D Nested Map Array:") |
79 | | -? |
80 | | -Dim a(1 To 10, 1 To 2) |
81 | | -a(5, 1) = Array("{x:1, y:3, down:{x:4, y:7}}") |
82 | | -? "a(5, 1) is: "; a(5, 1) |
83 | | -? "a(5, 1) is map? "; Ismap(a(5, 1)) |
84 | | -? "a is map? "; Ismap(a) |
85 | | -? |
86 | | -? "a(5, 1).x is: "; a(5, 1).x |
87 | | -? "a(5, 1).y is: "; a(5, 1).y |
88 | | -? "a(5, 1).down.x is: "; a(5, 1).down.x |
89 | | -? "a(5, 1).down.y is: "; a(5, 1).down.y |
90 | | -? |
91 | | -a(5, 1).down.x = "N/A" |
92 | | -? "a(5, 1).down.x is now: "; a(5, 1).down.x |
93 | | -? |
94 | | -?:?:? " Press any key..." |
95 | | -Pause |
96 | | -Cls |
97 | | -? uline("1-Dimension Map Array, with Two 1-D Nested Map Arrays:") |
98 | | -? |
99 | | -a = Array("{x:1, y:3, down:[{x:4, y:7}, {x:6, y:9}]}") |
100 | | -? "a is: "; a |
101 | | -? "a is map? "; Ismap(a) |
102 | | -? |
103 | | -? "a.x is: "; a.x |
104 | | -? "a.y is: "; a.y |
105 | | -? "a.down(0).x is: "; a.down(0).x |
106 | | -? "a.down(0).y is: "; a.down(0).y |
107 | | -? "a.down(1).x is: "; a.down(1).x |
108 | | -? "a.down(1).y is: "; a.down(1).y |
109 | | -? |
110 | | -a.down(1).x = "N/A" |
111 | | -? "a.down(1).x is now: "; a.down(1).x |
112 | | -? |
113 | | -?:?:? " Press any key..." |
114 | | -Pause |
115 | | -Cls |
116 | | -? uline("Create 1-Dimension Map Array without using ARRAY Keyword:") |
117 | | -? |
118 | | -b.x = 40 |
119 | | -b.y = 50 |
120 | | -b.title = "Top-Left" |
121 | | -? "b.x is: "; b.x |
122 | | -? "b.y is: "; b.y |
123 | | -? "b.title is: "; b.title |
124 | | -? |
125 | | -? "b is: "; b |
126 | | -? "b is map? "; Ismap(b) |
127 | | -? |
128 | | -?:?:? " Press any key..." |
129 | | -Pause |
130 | | -~~~ |
| 30 | +``` |
| 31 | +a = Array("[1,2,3]") |
| 32 | +print a ' Output: [1,2,3] |
| 33 | +``` |
0 commit comments